Introduction To PHP: Web Programming
Introduction To PHP: Web Programming
Web Programming
Background Knowledge
• Programming experience: Java/C#/C/C++…
• HTML -
http://www.w3schools.com/html/html_intro.asp
• CSS -
http://www.w3schools.com/css/css_intro.asp
Agenda
• How to install and configure the PHP engine
• Language fundamentals: variables, loops, strings, arrays...
• Functions
• Object - oriented applications
• Creating Web forms, and PHP scripts to handle them
• Interacting with browser cookies and creating sessions to store
visitor data
• File and directory handling
• Database - driven applications
• Dates and times, Web server environment, email messages
• Creating graphics with PHP
• Regular expressions
• How to read, write, and create XML documents with PHP
• Good programming practices, coding standards, documentation,
security issues, error handling, code separation, and code testing
History 1
• Created by Rasmus Lerdorf in 1994
▫ PHP (Personal Home Page)
▫ Set of simple tools coded in the C language
• PHP version 2, general public in 1995
• PHP 3 released in June 1998. Rasmus
Lerdorf, Zeev Suraski and Andi Gutmans
▫ Most of the code was rewritten
History 2
• PHP 4, was launched in May 2000
▫ PHP core rewritten again
▫ Zend Engine (Zeev and Andi)
▫ session handling features, output buffering, a
richer core language, wider variety of Web server
platforms…
▫ still suffered from a relatively poor OOP
implementation
History 3
• PHP 5, released in July 2004
▫ private and protected class members; final,
private, protected, and static methods; abstract
classes; interfaces; and a standardized
constructor/destructor syntax
• PHP 6, note released
• PHP 7, released in December 2015
▫ Under development
PHP - General
• PHP is a programming language for building
dynamic, interactive Web sites
▫ dynamic Web page is a page whose contents can
change automatically each time the page is viewed
▫ interactive Web site is a site that responds to input
from its visitors
• PHP programs run on a Web server, and serve Web
pages to visitors on request
• Embed PHP code within HTML Web pages, making
it very easy to create dynamic content quickly
PHP - General
• PHP is a server - side scripting language, which
means that PHP scripts, or programs, usually
run on a Webserver
▫ A good example of a client - side scripting
language is JavaScript, which commonly runs
within aWeb browser
• PHP is an interpreted language — a PHP script
is processed by the PHP engine each time it ’ s
run.
Running a PHP script
• A visitor requests a Web page by clicking a link, or
typing the page’s URL into the browser’s address bar
▫ The visitor might also send data to the Web server at
the same time
• The Web server recognizes that the requested URL
is a PHP script, and instructs the PHP engine to
process and run the script
• When the script is finished it usually sends an
HTML page to the Web browser, which the visitor
then sees on his screen
PHP tasks
• Reading and processing the contents of a Web form
sent by the visitor
• Reading, writing, and creating files on the Web
server
• Working with data in a database stored on the Web
server
• Grabbing and processing data from other Web sites
and feeds
• Generating dynamic graphics, such as charts and
manipulated photos
•…
Examples of PHP scripts
• Web forums that allow visitors to post messages and
discuss topics
• Search engines that let people search the contents of
a Web site or database
• Content management systems and blogs, which
enable Webmasters to create sites easily with
minimal technical knowledge
• Webmail applications, allowing people to send and
receive email using their Web browser
• Online stores, allowing shoppers to purchase
products and services over the Internet
Advantages of PHP
• Popularity
▫ large number of Internet service providers (ISPs) and
Web hosting companies support PHP
▫ hundreds of thousands of developers are using PHP
▫ several million sites are reported to have PHP
▫ Documentation www.php.net
• Cross – platform
▫ OS: Windows, Linux, FreeBSD, Mac OS X, Solaris…
▫ Web servers: Apache, Internet Information Server
(IIS), Zeus, lighttpd…
▫ Develop and test your PHP Web site on one setup,
then deploy it on a different type of system
Other WP technologies
• ASP (Active Server Pages)
• ASP.NET
• Perl
• Java
• Python
• Ruby
• Cold Fusion
PHP software
• Web server software, such as Apache or Internet
Information Server (IIS)
• PHP server module installed on the same
computer
▫ this module talks to the Web server software
▫ this is the PHP engine that actually does the work
of running your PHP scripts
• Optionally a database server
▫ MySQL, PostgreSQL, SQL Server…
Installation
• LAMP (Linux, Apache, MySQL, and PHP)
• WAMP (Windows, Apache, MySQL, and PHP)
▫ WampServer, http://www.wampserver.com/en/
▫ AppServ, https://www.appserv.org/en/
▫ XAMPP,
https://www.apachefriends.org/index.html
▫…
• MAMP (Mas OS, Apache, MySQL, and PHP)
▫ www.mamp.info/en/
Other ways to run PHP
• Running PHP with other Web Servers (IIS,
nginx, GWS…)
• Compiling PHP (with C compiler)
• Running PHP Remotely
First script
<?php
echo "Hello, world!";
?>
Embedding PHP within HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<title>Hello</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1><?php echo "Hello, world!"; ?></h1>
</body>
</html>
Dynamic script
<?php
$currentTime = date( "g:i:s a" );
echo "Hello, world! The current time is $currentTime";
?>
Comments
• Single line comments
// This code displays the current time
# This code displays the current time