Introduction to PHP 1.What is PHP?
What Is PHP? php: hypertext preprocessor Server-side scripting language—like ASP—scripts are executed on server Supports many databases (MySQL, Informix, Oracle, Sybase, PostgreSQL, Generic ODBC, etc.) Used to manipulate database on the Web
PHP File Can contain text, HTML tags and scripts When php code is executed on the server, results are returned to the browser as plain HTML Files have a file extension of ".php", ".php3", or ".phtml"
Basic PHP Syntax
Comments in PHP
Variables in PHP Starts with $, followed by alpha or “_”, followed by “A-Z”, “a-z”, or “0”-”9” Type is not declared but is determined by use E.g., $name = “John”; $age = 19; $address1 = “3140 Waialae Av”;
String Variables Given: $firstName = “Mary”; $lastName = “Poppins”; Catenation $fullName = $firstName. “ “. $lastName; echo $fullName; // prints Mary Poppins strLen() Function echo strLen($fullName; // prints 12
PHP Operators Arithmetic +, -, *, /, % Comparison ==, !=. <>, >, >=. <, <= Logical &&, ||, !
Conditional Statements if ($a % 2 == 0) echo $a. “ is even.”; If ($a % 2 == 0) echo $a. “ is even.”; else echo $a. “ is odd.”; if (($a > 0) && ($a % 2) == 0 ) { echo $a. “ is positive”; echo $a. “ is even”; }
Iteration Statements while (some condition is true) { statements; } do { statements; } while (some condtion is true) for ($i = 0; $i < $count; $i++){ statements; } foreach ($array as $value){ echo $value; }
Arrays $names = array(“Ann”, “Bob”, “Cathy”); $ages = array(); $ages[0] = 19; $ages[1] = 23; $ages[2] = 21; $count = 3; for ($i = 0; $i ”; }
Your Turn Write a php code which initializes parallel arrays $names and $ages with 3 values each and prints their contents in the following format: Mary is 21 years old. Tom is 19 years old. Harry is 22 years old.
Solution \n”; } ?>
Solution2 \n”; } echo $output; ?>
Associative Arrays Numeric Array E.g.: $ages = array(21, 19, 23, 25); 0 21, 1 19, 2 23, 3 25| Associative Array E.g. friends “ann” 21, “bob” 19, “cal” 23 E.g., eyeColors “ann” ”brown”, “bob” ”blue”, “cal” ”black”, “dan” ”green”
Initializing Associative Arrays // Example 1 $friends = array("Ann"=>19, "Bob"=>22, "Cathy"=>20); // Example 2 $eyeColors["Ann"] = "brown"; $eyeColors["Bob"] = "blue"; $eyeColors["Cathy"] = "black";
Printing Associative Array Given friends “ann” 21, “bob” 19, “cal” 23 echo "Ann is ". $friends["Ann"]. " years old. "; echo "Names and Ages "; print_r($friends); Output Ann is 21 years old. Array( [Ann] => 21 [Bob] => 19 [Cathy] => 23 )
Associative Array (another example) $entre = array (“Monday” => “turkey”, “Tuesday” => “pizza”, “Wednesday” => “beef”, “Thursday” => “chicken”, “Friday” => “fish”); $entre[“Saturday”] = “venison”; $entre[“Sunday”] = “ham”;
Associative Array (cont.) $size = count($entre) // $size contains 7 To print contents of $entre for ($n = 0; $n ”; }
for ($n = 0; $n ”; } each($entre) function -- $today = each($entre); Think of $today as 2-element associative array: $today = (key => “...”, value => “...)
Form Input (getinfo.html) Form Input (html) Name: Age:
Form Input (getinfo.php) Form Input (php) \n”; echo “Are you really “. $_POST[“myAge”]. “ years old? ”; ?>
Form Input (in one file) Form Input (php) \n”; echo “Are you really “. $_POST[“myAge”]; echo “ years old? ”; } ?> Name: Age:
Functions <?php function check($item) { if ($item == "") return false; else return true; } $name = $_POST["myName"]; $ = $_POST["my "]; if (check($name)) echo "Name: ". $name. " "; else echo "Please supply your name. "; if (check($ )) echo " ". $ . " "; else echo "Please supply your . "; ?>