Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.

Similar presentations


Presentation on theme: "Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server."— Presentation transcript:

1

2 Introduction to PHP 1.What is PHP?

3 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

4 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"

5 Basic PHP Syntax

6 Comments in PHP

7 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”;

8 String Variables  Given: $firstName = “Mary”; $lastName = “Poppins”;  Catenation $fullName = $firstName. “ “. $lastName; echo $fullName; // prints Mary Poppins  strLen() Function echo strLen($fullName; // prints 12

9 PHP Operators  Arithmetic +, -, *, /, %  Comparison ==, !=. <>, >, >=. <, <=  Logical &&, ||, !

10 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”; }

11 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; }

12 Arrays $names = array(“Ann”, “Bob”, “Cathy”); $ages = array(); $ages[0] = 19; $ages[1] = 23; $ages[2] = 21; $count = 3; for ($i = 0; $i ”; }

13 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.

14 Solution \n”; } ?>

15 Solution2 \n”; } echo $output; ?>

16 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”

17 Initializing Associative Arrays // Example 1 $friends = array("Ann"=>19, "Bob"=>22, "Cathy"=>20); // Example 2 $eyeColors["Ann"] = "brown"; $eyeColors["Bob"] = "blue"; $eyeColors["Cathy"] = "black";

18 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 )

19 Associative Array (another example)  $entre = array (“Monday” => “turkey”, “Tuesday” => “pizza”, “Wednesday” => “beef”, “Thursday” => “chicken”, “Friday” => “fish”);  $entre[“Saturday”] = “venison”; $entre[“Sunday”] = “ham”;

20 Associative Array (cont.)  $size = count($entre) // $size contains 7  To print contents of $entre for ($n = 0; $n ”; }

21  for ($n = 0; $n ”; }  each($entre) function -- $today = each($entre); Think of $today as 2-element associative array: $today = (key => “...”, value => “...)

22

23 Form Input (getinfo.html) Form Input (html) Name: Age:

24 Form Input (getinfo.php) Form Input (php) \n”; echo “Are you really “. $_POST[“myAge”]. “ years old? ”; ?>

25 Form Input (in one file) Form Input (php) \n”; echo “Are you really “. $_POST[“myAge”]; echo “ years old? ”; } ?> Name: Age:

26 Functions <?php function check($item) { if ($item == "") return false; else return true; } $name = $_POST["myName"]; $email = $_POST["myEmail"]; if (check($name)) echo "Name: ". $name. " "; else echo "Please supply your name. "; if (check($email)) echo "email: ". $email. " "; else echo "Please supply your email. "; ?>


Download ppt "Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server."

Similar presentations


Ads by Google