Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.

Similar presentations


Presentation on theme: "CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1."— Presentation transcript:

1 CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1

2 Content PHP 5 if...else...elseif Statements PHP 5 switch Statement PHP 5 while Loops PHP 5 for Loops PHP 5 Functions PHP 5 Arrays PHP 5 Sorting Arrays PHP 5 Global Variables - Superglobals 2

3 Content PHP 5 Form Handling PHP 5 Form Validation PHP 5 Forms - Required Fields PHP 5 Forms - Validate E-mail and URL PHP 5 Complete Form Example 3

4 PHP 5 if...else...elseif Statements PHP - The if Statement 4 PHP - The if...else Statement <?php $t = 15; if ($t < 20) { echo "Have a good day!"; }else{ echo "Have a good night!"; } ?>

5 PHP 5 if...else...elseif Statements (cont.) PHP - The if...elseif....else Statement 5

6 PHP 5 switch Statement The PHP switch Statement 6

7 PHP 5 while Loops The PHP while Loop "; $x++; } ?> 7 The PHP do...while Loop "; $x++; } while ($x

8 PHP 5 for Loops The PHP for Loop "; } ?> 8 The PHP foreach Loop "; } ?>

9 PHP 5 Functions Create a User Defined Function in PHP Syntax function functionName() { code to be executed; } Example 9

10 PHP 5 Functions (cont.) PHP Function Arguments Example "; } familyName("Jani"); familyName("Hege"); //output Jani Hege ?> 10

11 PHP 5 Functions (cont.) PHP Default Argument Value Example "; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?> 11

12 PHP 5 Functions (cont.) PHP Functions - Returning values Example "; echo "7 + 13 = ". sum(7, 13). " "; echo "2 + 4 = ". sum(2, 4); ?> 12

13 PHP 5 Arrays What is an Array? An array is a special variable, which can hold more than one value at a time. $cars1 = "Volvo"; $cars2 = "BMW"; $cars3 = "Toyota"; However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? 13

14 PHP 5 Arrays (cont.) Create an Array in PHP array(); Example $cars = array("Volvo", "BMW", "Toyota"); or the index can be assigned manually: $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota"; 14

15 PHP 5 Arrays (cont.) Get The Length of an Array - The count() Function count(); Example //output 3 15

16 PHP 5 Arrays (cont.) Loop Through an Indexed Array Example "; } ?> 16

17 PHP 5 Arrays (cont.) PHP Associative Arrays Example "35", "Ben"=>"37", "Joe"=>"43"); echo "Peter is ". $age['Peter']. " years old."; ?> 17 Loop Through an Associative Array Example "35", "Ben"=>"37", "Joe"=>"43"); foreach($age as $x => $x_value) { echo "Key=". $x. ", Value=". $x_value; echo " "; } ?>

18 PHP 5 Sorting Arrays PHP - Sort Functions For Arrays In this chapter, we will go through the following PHP array sort functions: sort() - sort arrays in ascending order rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key 18

19 PHP 5 Sorting Arrays (cont.) Sort Array in Ascending Order - sort() 19 Sort Array in Descending Order - rsort()

20 PHP 5 Sorting Arrays (cont.) Sort Array (Ascending Order), According to Value - asort() "35", "Ben"=>"37", "Joe"=>"43"); asort($age); ?> 20 Sort Array (Ascending Order), According to Key - ksort() "35", "Ben"=>"37", "Joe"=>"43"); ksort($age); ?>

21 PHP 5 Sorting Arrays (cont.) Sort Array (Descending Order), According to Value - arsort() "35", "Ben"=>"37", "Joe"=>"43"); arsort($age); ?> 21 Sort Array (Descending Order), According to Key - krsort() "35", "Ben"=>"37", "Joe"=>"43"); krsort($age); ?>

22 PHP 5 Global Variables - Superglobals PHP Global Variables - Superglobals Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. The PHP superglobal variables are: $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_ENV $_COOKIE $_SESSION 22

23 PHP 5 Global Variables - Superglobals (cont.) PHP $_POST "> Name: 23

24 PHP 5 Global Variables - Superglobals (cont.) PHP $_GET //test_get.html Test $GET 24 PHP $_GET //test_get.php

25 PHP 5 Form Handling PHP - A Simple HTML Form $_GET Name: E-mail: 25 PHP - A Simple HTML Form Welcome Your email address is: // output Welcome name Your email address is name@example.com The PHP superglobals $_GET and $_POST are used to collect form-data.

26 PHP 5 Form Handling PHP - A Simple HTML Form $_POST Name: E-mail: 26 PHP - A Simple HTML Form Welcome Your email address is: // output Welcome name Your email address is name@example.com

27 PHP 5 Form Validation 27

28 PHP 5 Form Validation (cont.) 28 // formValidate.php <?php $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $website = test_input($_POST["website"]); $comment = test_input($_POST["comment"]); $gender = test_input($_POST["gender"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>

29 PHP 5 Form Validation (cont.) 29 // formValidate.php PHP Form Validation Example "> Name: E-mail: Website: Comment: Gender: Female Male

30 PHP 5 Form Validation (cont.) 30 // formValidate.php Your Input: "; echo $name; echo " "; echo $email; echo " "; echo $website; echo " "; echo $comment; echo " "; echo $gender; ?>

31 PHP 5 Forms - Required Fields 31 // formValidate.php <?php $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); } if (empty($_POST["email"])) { $emailErr = "Email is required"; } else { $email = test_input($_POST["email"]); }

32 PHP 5 Forms - Required Fields (cont.) 32 // formValidate.php if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = test_input($_POST["gender"]); } } ?>

33 PHP 5 Forms - Required Fields (cont.) 33 // formValidate.php "> Name: * E-mail: * Website: Comment: Gender: Female Male *

34 PHP 5 Forms - Validate E-mail and URL 34 // formValidate.php PHP - Validate Name $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } // formValidate.php PHP - Validate E-mail $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "Invalid email format"; }

35 PHP 5 Forms - Validate E-mail and URL (cont.) 35 // formValidate.php PHP - Validate URL $website = test_input($_POST["website"]); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0- 9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = "Invalid URL"; }

36 PHP 5 Complete Form Example 36 // formValidate.php PHP - Keep The Values in The Form Name: "> E-mail: "> Website: "> Comment: Gender: value="female">Female value="male">Male

37 37 THE END


Download ppt "CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1."

Similar presentations


Ads by Google