PHP: Hypertext PHP: Hypertext PreprocessorPreprocessor
Greg LoweGreg LoweRob WhiteRob White
Brian WrightBrian WrightMike Zywiec Mike Zywiec
IntroductionIntroduction
PHP stands for “PHP: Hypertext PHP stands for “PHP: Hypertext Preprocessor”Preprocessor”
Server-side scripting languageServer-side scripting language
Syntactically similar to CSyntactically similar to C
Utilized by approximately 33% of web Utilized by approximately 33% of web domains on the webdomains on the web
HistoryHistory
1995 – Rasmus Lerdorf wrote the Personal 1995 – Rasmus Lerdorf wrote the Personal Home Page/Form Interpreter: PHP/FIHome Page/Form Interpreter: PHP/FIWanted to create a HTML-centric language as Wanted to create a HTML-centric language as opposed to PERL which was PERL-centricopposed to PERL which was PERL-centricCode was inside the HTML document and Code was inside the HTML document and written within <!-- --> Tagswritten within <!-- --> TagsExample: Example:
<!--getenv HTTP_USER_AGENT--><!--getenv HTTP_USER_AGENT--><!—ifsubstr $exec_result Mozilla--> <!—ifsubstr $exec_result Mozilla--> Hey, you are using Netscape!<p> Hey, you are using Netscape!<p> <!--endif--> <!--endif-->
HistoryHistory
Lerdorf was not satisfied with PHP/FI so Lerdorf was not satisfied with PHP/FI so he wrote PHP/FI v.2he wrote PHP/FI v.2
Embedded commands within <? > tagsEmbedded commands within <? > tags
Same syntax used todaySame syntax used today
HistoryHistory
1997—Zeev Suraski and Andi Gutmans 1997—Zeev Suraski and Andi Gutmans contacted Lerdorf in an effort to combine their contacted Lerdorf in an effort to combine their already written engine with PHP/FI v.2already written engine with PHP/FI v.2After agreeing, the three and some others After agreeing, the three and some others finished the release of PHP v.3finished the release of PHP v.3PHP v.3 gained:PHP v.3 gained: Limited Object Oriented SupportLimited Object Oriented Support More speedMore speed Numerous DB support (MySQL, ODBC, Oracle, …)Numerous DB support (MySQL, ODBC, Oracle, …) ExtensibilityExtensibility
Allowed for programmers to write their own modules for the Allowed for programmers to write their own modules for the language language
HistoryHistory
Zeev and Andy went to work on what is Zeev and Andy went to work on what is now called the Zend Enginenow called the Zend Engine
The Zend Engine + PHP v3 = PHP4(2000)The Zend Engine + PHP v3 = PHP4(2000)
HistoryHistory
PHP v. 5 -- 2004PHP v. 5 -- 2004 Better Objected Oriented SupportBetter Objected Oriented Support Better Error CheckingBetter Error Checking
Java Style try{} catch{}Java Style try{} catch{} SimpleXML SimpleXML
Easy interaction with XML DocumentsEasy interaction with XML Documents
Installing PHPInstalling PHP
Web EnvironmentWeb Environment
Command Line InterfaceCommand Line Interface
Command Line PHP ScriptingCommand Line PHP Scripting
Can execute code from command line in Can execute code from command line in three ways:three ways: Executing fileExecuting file Code from command lineCode from command line Passed code from standard inputPassed code from standard input
Advantages of CLI InstallationAdvantages of CLI Installation
Same code can work on Unix or Windows Same code can work on Unix or Windows machinesmachines ‘‘#!/usr/bin/php’#!/usr/bin/php’
Web-independent developmentWeb-independent development
For Server-Side ScriptingFor Server-Side Scripting
Prepackaged solutionsPrepackaged solutions
Manually compiling & configuring Manually compiling & configuring
Works for Apache 1.3.x/2.0, IISWorks for Apache 1.3.x/2.0, IIS
Known IssuesKnown Issues
IISIIS Delicacy (Security)Delicacy (Security)
Apache Apache 2.0 (Thread safety, high-traffic areas)2.0 (Thread safety, high-traffic areas)
PHP Strengths:PHP Strengths:
Ready to use tools to interface with many Ready to use tools to interface with many databases and web utilities. (MySQL, databases and web utilities. (MySQL, Oracle, Etc.) Oracle, Etc.)
Easily extensible. PHP is modular and it is Easily extensible. PHP is modular and it is simple to create utilities to extend the simple to create utilities to extend the capabilities of the language. capabilities of the language.
PEAR project (PEAR project (http://http://pear.php.netpear.php.net) )
PHP Strengths:PHP Strengths:
Server Side processing Server Side processing
No client side software No client side software
PHP Weaknesses:PHP Weaknesses:
Weakly typed variables. Weakly typed variables.
Poor error detection / warning. Poor error detection / warning.
Really only suitable for web applications.Really only suitable for web applications.
PHPPHP<?php <?php
// Print a greeting if the form was submitted // Print a greeting if the form was submitted if ($_POST['user']) { if ($_POST['user']) {
print "Hello, ";print "Hello, "; // Print what was submitted in the form parameter called 'user' // Print what was submitted in the form parameter called 'user' print $_POST['user']; print $_POST['user']; print "!"; print "!";
} else { } else { // Otherwise, print the form // Otherwise, print the form print <<<_HTML_ print <<<_HTML_ <form method="post" action="$_SERVER[PHP_SELF]"> <form method="post" action="$_SERVER[PHP_SELF]"> Your Name: <input type="text" name="user"> Your Name: <input type="text" name="user"> <br/> <br/> <input type="submit" value="Say Hello"><input type="submit" value="Say Hello"></form> _HTML_; </form> _HTML_;
} } ?> ?>
PHPPHP
<form method="post" action="/~gl1042/PHP/example-1-05.php"> <form method="post" action="/~gl1042/PHP/example-1-05.php">
Your Name: <input type="text" name="user"> Your Name: <input type="text" name="user">
<br/> <br/>
<input type="submit" value="Say Hello"> <input type="submit" value="Say Hello">
</form> </form>
Then when I click on enter the returned information is:Then when I click on enter the returned information is:
Hello, greg!Hello, greg!
Example-1-05.phpExample-1-05.php
PHPPHPrequire 'DB.php';require 'DB.php';
$db = B::connect('mysql://hunter:w)[email protected]/restaurant');$db = B::connect('mysql://hunter:w)[email protected]/restaurant');
if (DB::isError($db)) if (DB::isError($db)) { {
die("connection error: " . $db->getMessage( )); die("connection error: " . $db->getMessage( )); }}
// Eggplant with Chili Sauce is spicy// Eggplant with Chili Sauce is spicy$db->query("UPDATE dishes SET is_spicy = 1 $db->query("UPDATE dishes SET is_spicy = 1 WHERE dish_name = 'Eggplant with Chili Sauce");WHERE dish_name = 'Eggplant with Chili Sauce");
// Lobster with Chili Sauce is spicy and pricy// Lobster with Chili Sauce is spicy and pricy$db->query("UPDATE dishes SET is_spicy = 1, price=price * 2$db->query("UPDATE dishes SET is_spicy = 1, price=price * 2 WHERE dish_name = 'Lobster with Chili Sauce'"); WHERE dish_name = 'Lobster with Chili Sauce'");
PHPPHP
<?php<?php
print 'strftime() says: ';print 'strftime() says: ';
print strftime('%c');print strftime('%c');
print "\n";print "\n";
print 'date() says:';print 'date() says:';
print date('r');print date('r');
?>?>
1example-9-011example-9-01
PHPPHP
<?php<?phpfunction page_header2($color) {function page_header2($color) { print '<html><head><title>Welcome to my print '<html><head><title>Welcome to my
site</title></head>';site</title></head>'; print '<body bgcolor="#' . $color . '">';print '<body bgcolor="#' . $color . '">';}}page_header2('cc00cc');}page_header2('cc00cc');}?>?>
1example-5-04.php1example-5-04.php
PHPPHP
<?php phpinfo(); ?><?php phpinfo(); ?>
Example-A-03.phpExample-A-03.php
PHPPHP