Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web.

Similar presentations


Presentation on theme: "Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web."— Presentation transcript:

1 Web Technology Introduction to PHP

2 PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web server. PHP can be embedded in HTML or used as a standalone binary PHP’s syntax is C-like and PHP’s variables are Perl-like

3 Insert PHP Codes in HTML My first HTML document <?php echo “Hello World!!”; ?>

4 Variable in PHP <? $temperature = 28.9; echo “ ”; echo “Today temperature is “.$temperature.” degrees.”; echo “ ”; $score = 88; echo “ My score is $score. ”; ?> PHP variables are not strictly typed All variables are started with “$” (dollar sign). There is no need to declare variables before using. String concatenation is done by “.” (dot).

5 Variable in PHP (cont) $arr = array(1, 3, 5, 20); echo “$arr[2]”; Array variables are loosely defined. We can assign a value to the array directly. $arr[0] = “Apple”; $arr[1] = “Mango”; $arr[4] = “Grape”; We can also initialize array variable using array() construct.

6 Associative Array Array that stores element values in association with key values rather than a strict linear index order. The element ‘Bang Sue’ is stored in the array $location, in association with lookup key ‘KMUTNB’. Another example: The result of variable $my_arr has three values (1, 2, 3), each is stored in association with keys (0, ‘orange’, and 3) $location[‘KMUTNB’] = ‘Bang Sue’; $my_arr[0] = 1; $my_arr[‘orange’] = 2; $my_arr[3] = 3;

7 Branching <? $score = 55; if($score>=50) echo “You pass.”; else echo “You fail.”; ?> if-else if (test) statement-1; else statement-2; if (test) { statement…; ………….; } else { statement….; …………..; }

8 Branching switch($shape) { case 0: echo “Circle.”; break; case 1: echo “Triangle; break; case 2: echo “Rectangle; break; default: echo “Polygon”; } Switch-Case switch(expression) { case value-1: statement; ……. [break;] case value-2: statement; ……. [break;] [default:] default-statement; ……. }

9 Looping (Iteration) for($i=0; $i<10; $i++) echo “$i: Hello World! ”; for loop for (initial-expression; termination-check; loop-end-expression) statement; for (initial-expression; termination-check; loop-end-expression) { statement; ………….. }

10 Looping (Iteration) $i=0; while($i<10) { echo “$i: Hello World! ”; $i++ } while loop while(condition) statement; while(condition) { statement; ………….. }

11 Looping (Iteration) $i=0; do { echo “$i: Hello World! ”; $i++ } while($i<10); do-while loop do statement while (expression); do { statement; ……….; } while (expression);

12 Web Technology HTML and PHP

13 Create Form in HTML …Form’s Objects….. ……………………….. Action: Specify the target php file to process this form method: GET: send data by appending them to the URL POST: send data with the body of form Data not visible to user

14 Set Variable Name to Form’s Objects Text Select Radio Button (use same names for all in the group) Check Box (use array[] for all in the group) Male Female Sports Books Music

15 Example of a Form TE TM TCT TTC === Highest Degree === Bachalor Master Doctoral Music Sports Books

16 Example of the Form

17 Retrieving Values from the Form in PHP If the method is “GET” If the method is “POST” We can use $_REQUEST[‘varname’] to get value, regardless of the method used in the form Let’s try this “phpinfo();”; <? echo $_GET[‘varname’]; ?> <? echo $_POST[‘varname’]; ?>

18 Retrieving Values from the Form in PHP Retrieving Values from a Text, Radio Button, and select is easy. Just refer to the object’s name (variable name) we want to retrieve the value from <? echo $_POST[‘txtName’].” ”; echo $_POST[‘rdDept’].” ”; echo $_POST[‘sltDegree’].” ”; ?>

19 Retrieving Values from the Form in PHP Retrieving values from checkbox is a bit tricky We never know how many items would be checked –It is also possible that no items be selected at all <?php for($i=0; $i < sizeof($_POST['chkHobby']); $i++) echo $_POST['chkHobby'][$i]." "; ?> $hobby = $_POST[‘chkHobby’]; for($i=0; $i < sizeof($hobby); $i++) echo $hobby[$i]." "; or

20 So! What else? Make sure we validate data before submitting –Use javascript to check the validity of data before sending them to the target PHP –This is to make data ready for processing in PHP How can we make the form look nicer? Use or to help display better output What happens if method is “GET”, instead? Try to play with $_REQUEST[‘xxx’], see what happens? What happens if we want to store these values in the database?


Download ppt "Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web."

Similar presentations


Ads by Google