1 / 67

Chapter 2: PHP Language (I)

Chapter 2: PHP Language (I). Overview. Introduction Getting Start Writing PHP Variables & Constants Data Types Operators Flow control Functions. Introduction. PHP: Hypertext Preprocessor A widely-used Open Source general-purpose scripting language.

teagan
Download Presentation

Chapter 2: PHP Language (I)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: PHP Language (I)

  2. Overview • Introduction • Getting Start • Writing PHP • Variables & Constants • Data Types • Operators • Flow control • Functions

  3. Introduction • PHP: Hypertext Preprocessor • A widely-used Open Source general-purpose scripting language. • Originally designed to create dynamic web pages, PHP's principal focus is server-side scripting. • PHP scripts can be embedded into HTML. • The LAMP architecture has become popular in the Web industry as a way of deploying inexpensive, reliable, scalable, secure web applications. • PHP is commonly used as the P in this bundle alongside Linux, Apache and MySQL. • FAMP replaces Linux with FreeBSD, WAMP replaces Linux with Windows. • Key Point: • Server-side, scripting language.

  4. Getting Start (1) • Put the php code into your html documents. • Generally, the PHP code is embedded in html tags. • These special tags are <? ……… ?> <?php ……… ?> • You can also make it using “<script>” tag <script language=“php”> …… …… </script> • Generally the filename ends with “.php”, “.php5”, or “.phtml”

  5. Getting Start (2) • Example: <html> <head><title> Welcome to PHP Pages</title></head> <body> <?php for( $i = 1 ; $i <= 4 ; $i++){ echo "<font size=$i><p>Font Size: $i - "; echo "Welcome Using PHP Language </p></font>\n"; } ?> </body> </html>

  6. PHP Server Web Pageswith PHP Code HTML Docs Getting Start (3) • Server-side processing • Viewing the Output: [檢視]  [檢視原始檔] <html> <head><title> Welcome to PHP Pages</title></head> <body> <font size=1><p>Font Size: 1 - Welcome Using PHP Language </p></font> <font size=2><p>Font Size: 2 - Welcome Using PHP Language </p></font> <font size=3><p>Font Size: 3 - Welcome Using PHP Language </p></font> <font size=4><p>Font Size: 4 - Welcome Using PHP Language </p></font> </body> </html> <html> <head><title> Welcome to PHP Pages</title></head> <body> <?php for( $i = 1 ; $i <= 4 ; $i++){ echo "<font size=$i><p>Font Size: $i - "; echo "Welcome Using PHP Language </p></font>\n"; } ?> </body> </html>

  7. Getting Start (3) • Practicing • Writing Your PHP “Hello World” <html> <head><title> Hello World to PHP Pages</title></head> <body> <?php echo "Hello World"; ?> </body> </html>

  8. Writing PHP (1) • PHP Example • “;” – end of statements • “//” – comment line • /* ……… */ – comment several lines <?php // for loop for( $i = 1 ; $i <= 4 ; $i++) { /* * display different font-size welcome messages */ echo "<font size=$i>Font Size: $i - "; echo "Welcome</font><br>\n"; } ?>

  9. Writing PHP (1) • PHP Example • “.” – string concatenation <html> <head><title> Hello World to PHP Pages</title></head> <body> <?php echo "Welcome Using"." PHP Language"; ?> </body> </html>

  10. Writing PHP (2) • Practicing • Writing a PHP program to show welcome message. • Using strings concatenation your welcome message. • Adding some comment to explain what to do? <html> <head><title>Hello World to PHP Pages</title></head> <body> <?php // say hello world echo "Hello"." World"; ?> </body> </html>

  11. Variables & Constants (1) • Variables • To store some data in program execution • Need no declaration in php. • Naming of Variables • Starting with alphabets (a-zA-Z) or underline (“_”) • After that, alphabets, numbers, and underlines are allowed, without length constrains. • Ex. – N, n, size, a10, short_v1, _help, _4tmp, Count ※Note: variable names are case sensitive. • ‘Hello’ is different with ‘hEllo’ • Indicating Variables • Adding ‘$’ in front of variable name • Ex. – $N, $n, $size, $a10, $short_v1, $_help, $_4tmp, $Count

  12. Variables & Constants (2) • Value Assignment • By Value $name = “Tsung-Hsi Weng”; // string $score = 80; // integer $id = “1230017”; // string • By Variable $nickname = $name; // copy values in $name into $nickname // $nickname will not change with $name • By Reference $sid = &$id; // reference assignment (like pointer in C) // $sid will be changed while changing $id ※Data type of one variable changes with what it is assigned. $x = 85; // $x is integer here. $x = “127”; // now, $x is a string.

  13. Variables & Constants (3) • Outputting Variable in PHP • Language Constructs: echo(), print() • To output something you want to. • Not actually real functions. • Can be used with or without “()”. • Difference between echo & print: • Variables in echo can be separated with “,”; echo “Hello World<br>\n”; echo “Hello ” . “World” . “<br>\n”; echo “Hello ” , “World” , “<br>\n”; • Simplified written <? echo $var ?> <?=$var ?>

  14. Variables & Constants (4) • Practicing • Adding some variables in your php to store your personal information, and printout the variable result. • Changes value in them, and printout it again. <html> <head><title>Hello</title></head> <body> <?php $name = "Peter"; $lang = "PHP"; echo "Hello. My name is $name. "; echo "This page is produced by $lang. "; $name = "Bill"; echo "$name is my friend."; ?> </body> </html>

  15. Variables & Constants (5) • Dynamic Variable • “Dynamic Variable” is easy to be implemented in interpreted languages. • PHP: Variable Variable Name <html> <head> <title> Dynamic Variable In PHP </title> </head> <body> <?php $a="b"; $b="c"; $c="d"; echo '$a=', $a, "<br>\n"; // $a=b echo '$$a=', $$a, "<br>\n"; // $$a=c echo '$$$a=', $$$a, "<br>\n"; // $$$a=d ?> </body> </html>

  16. Variables & Constants (6) • Constants • Naming: similar to variables • Indicating: just use the name of constants • Also case sensitive • Defining a constant • Syntax: define (“CONSTNAME”, VALUE); define (“PI”, 3.1415926); define (“TEACHER”, “Tsung-Hsi Weng”); • Predefined constants

  17. Variables & Constants (7) • Example <html> <head> <title>Constants In PHP </title> </head> <body> <?php define ("PI", 3.1415926); echo "This line number before 'PI' is ".__LINE__."<br>"; echo "PI is ".PI.". <br>"; echo "This line number after 'PI' is ".__LINE__; ?> </body> </html>

  18. Data Types (1) • 8 basic data types • boolean, integer, float, string , NULL, array, object, resource • Boolean • True / False • Case insensitive (TRUE = True) • Usually used in logical operation. • $percentage>=0.5 will be true or false. • Integer • Range: -2147483648 ~ 2147483647 (32 bits) • Expression • Decimal: 5678, -1234 • Octal: 0234, -0135 • Hexadecimal: 0x1A, -0x3fc • Floating Point Number (float) • Range: 4.94065645841246544e-324 ~ 1.79769313486231570e308 (64 bits) • Ex. 2.345, 1.2e3 (=1200), 7E-4 (=0.0007)

  19. Data Types (2) • String • Characters connected, quoted with ‘’ or “” • Ex. ‘This is a string’, “This is a string, too.” • Difference between single quote (‘’) and double quote (“”) • Double quote will do variable exchange, but single quote do not. $user = “chwong”; echo "$user"; // output chwong echo '$user'; // output $user • Escape characters – \ \n – LF, Line Feed. (New Line) \r – CR, Carriage Return. (ENTER) \t – TAB \\ –「\」 \$ –「$」 \” –「”」 \’ –「’」

  20. Data Types (3) • NULL • Only one value: NULL • Case insensitive (NULL = null) • Cases • Undefined variables • Variables assigned to NULL • Variable unsetted. (using unset() ) • Array, Object, Resource • Not mentioned here.

  21. Data Types (4) • Practicing • Adding three integer variables and assigning to 100 with three different expressions, and printout individual result. • Adding three float variables and assigning to 314.16 with three different expressions, and printout individual result. <?php $int1 = 100; $int2 = 0x64; $int3 = 0144; $str1 = "int1 = $int1, int2 = $int2, int3 = $int3<br>"; $ft1 = 314.16; $ft2 = 3.1416e2; $ft3 = 31416E-2; $str2 = "ft1 = $ft1, ft2 = $ft2, ft3 = $ft3”; echo "$str1<br>$str2"; ?>

  22. Data Types (5) • Type Conversions • Automatically • Operations with different types of operands. • Example • Cast Operator • Syntax: (data type) expression or variable • Example $a=17; $b=5; $c = (int) ($a / $b); // $c=3 echo (string) $a . (string) $b; // output 175

  23. Data Types (6) • Practicing • Adding variable pi and assigning string value “3.146” and compute circle area with radius equal 3 unit. Finally printout the result used integer. • Using cast operator. <html> <head> <title>Conversion Data Type</title> </head> <body> <?php $pi = "3.1416"; $radius = 3; $area = $pi * ($radius * $radius); echo "Radius = $radius, Circle area is ",(int)$area; ?> </body> </html>

  24. Operators (1) • Types of operators • Arithmetic, Comparison, Logical, Bitwise, Combined Operators, String Concatenation, Error Control • Priorities • Ex: <html> <head> <title>Conversion Data Type</title> </head> <body> <?php echo 2 * 3 + 1 * (1 + 6 / 2); //10 ?> </body> </html>

  25. Operators (2) • Arithmetic ( + - * / % ++ --) • Ex. $a=5; $b=4; $c = -$a; // $c = -5 $c = $a + $b; // $c = 9 $c = $a - $b; // $c = 1 $c = $a * $b; // $c = 20 $c = $a / $b; // $c = 1.25 $c = $a % $b; // $c = 1 echo $a++; // output 5, $a = 6 echo ++$b; // $b = 5, output 5 $b--; // $b = 4

  26. Operators (3) • Comparison ( > < == >= <= === != ) • Results: True / False • Ex. $a = 123; $b = "456"; $c = $a > $b; echo $c; // False, display NULL string $c = $a < $b; echo $c; // True, display 1 $c = $a === $b; echo $c; // False, display NULL string $c = ($a = $b) == $b; echo $c; // True, display 1 $c = $a < $b; echo $c; // False, display NULL string $c = $a <= $b; echo $c; // True, display 1 $c = $a >= $b; echo $c; // True, display 1 $c = $a != $b; echo $c; // False, display NULL string $c = $a === $b; echo $c; // True, display 1

  27. Operators (4) • Logical ( && || ! and or xor) • Results: True / False • Ex. $a = 123; $b = "456"; $c = $a < 100 && $b > 400; // $c = false $c = $a == 123 && $b == 456; // $c = true $c = $a > 200 || $b > 200; // $c = true // and/or/xor priority is higher then assign operator (=) $c = $a > 200 or $b > 200; // $c = false $c = $a == 456 and $b == 456; // $c = false $c = !($a == 456) and $b == 456; // $c = true $c = !($a == 456) xor $b == 456; // $c = true $c = ($a < 100 xor $b > 400); // $c = true $c = ($a > 100 xor $b > 400); // $c = false

  28. Operators (5) • Bitwise ( ~ & | ^ << >>) • Ex. $a = 1; $a = 192 & 0x80; // $a = 128; $a = 192 & ~0x80; // $a = 64; $a = 1 | 3 | 4; // $a = 7; $a = 1 << 2; // $a = 4; $a = -1 << 2; // $a = -4; $a = 256 >> 2; // $a = 64; $a = -256 >> 2; // $a = -64;

  29. Operators (6) • Combined ( += -+ *= /= %= .= <<= >>= &= |= ^=) • Ex. $a=5; $b=4; $a += $b; // $a = 9 $a *= $b; // $a = 36 $a -= $b; // $a = 32 $a /= $b; // $a = 8 $a %= $b; // $a = 0 $a .= $b; // $a = “04” $a <<= 2; // $a = 16 $a >>= 1; // $a = 8 $a |= 3; // $a = 11 $a ^= 12; // $a = 7 $a &= 4; // $a = 4

  30. Operators (7) • String Concatenation ( . ) • Ex. <html> <head><title> Hello World to PHP Pages</title></head> <body> <?php echo "Welcome Using"." PHP Language"; ?> </body> </html>

  31. Operators (8) • Error Control ( @ ) • Ex. echo ( 5 / 2 ), "\n"; // output 2.5 echo ( 5 / 0 ), "\n"; // Warning: Division by zero in filename on line # echo @ ( 5 / 2 ), "\n"; // output 2.5 echo @ ( 5 / 0 ), "\n"; // output nothing

  32. Flow Control (1) • Flow Controls • Conditional • if … elseif … else … • switch … case … default… • ?: • Loop • for loop • foreach loop • while loop • do/while loop • require/require_once • include/include_once

  33. Code before false condition true Code in if-block Code after Flow Control (2) • Conditional • if code before; if ( condition ) { code in if-block; } code after; • Example if ( $score < 60 ) { echo “You cannot pass!”; }

  34. Code before true false condition Code inif-block Code inelse-block Code after Flow Control (3) • if … else code before; if ( condition ) { code in if-block; }else { code in else-block; } code after; • Example if ( $score < 60 ) { echo “You cannot pass!”; } else { echo “Congratulations!”; }

  35. Flow Control (4) • ?: – ternary operator condition ? expression1 : expression2; • Same as if (condition) { expression1 } else { expression2 } • Example echo $score < 60 ? "You cannot pass!" : "Congratulations!";

  36. Code before true false condition true false condition2 Code inif-block Code inelseif-block Code inelse-block Code after Flow Control (5) • if … elseif … else code before; if ( condition ) { code in if-block; }elseif (condition2) { code in elseif-block; }else { code in else-block; } code after; • Example repeatable if ( $score > 90 ) { echo “You are so great!”; } elseif ( $score > 60 ) { echo “Nice grade.”; } else { echo “You cannot pass!”; }

  37. Flow Control (6) • switch-case switch ( variable ) { case value1: code when value1; break; case value2: code when value1; break; default: code when other; }

  38. Flow Control (7) • Example Switch ( $option ) { case 1: echo “You select option 1”; break; case 2: echo “You select option 2”; break; default : echo “I don’t what did you select”; break; }

  39. Flow Control (8) • Practicing • Adding a variable which store 0~6 is individual mean Sunday to Saturday, printout the variable which mean. • Using one of switch-case or if-else in php. <?php $day = 3; switch ($day) { case 0: echo "Sunday"; break; ... ... case 6: echo "Saturday"; break; default: echo "what day is today?"; } ?>

  40. Flow Control (9) • Loops • for loop for (initial expression; condition expression; end expression) { statements } • The initial expression is evaluated (executed) once unconditionally at the beginning of the loop. • In the beginning of each iteration, condition expression is evaluated. • If it evaluates to TRUE, the loop continues and the nested statements are executed. If it evaluates to FALSE, the execution of the loop ends. • At the end of each iteration, end expression is evaluated (executed).

  41. Code before Initial expression loop false true condition Code infor-loop-block End expression Code after Flow Control (10) • for loop

  42. Flow Control (11) • for loop <html> <head> <title>Flow Control</title> </head> <body> <?php $sum = 0; for ($i = 1; $i <= 10; $i++) { $sum += $i; } echo "Sum 1 to 10 is $sum"; ?> </body> </html>

  43. Flow Control (12) • while while (condition) { statements } • while loops are the simplest type of loop in PHP • It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. • The value of the expression is checked each time at the beginning of the loop.

  44. Code before loop false true condition Code inwhile-block Code after Flow Control (13) • while

  45. Flow Control (14) • while <html> <head> <title>Flow Control</title> </head> <body> <?php $sum = 0; $i = 1; while ($i <= 10) { $sum += $i; $i++; } echo "Sum 1 to 10 is $sum"; ?> </body> </html>

  46. Flow Control (15) • do-while do { statements } while (condition); • do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. • the truth expression is only checked at the end of the iteration, if it evaluates to FALSE, then the loop execution ends.

  47. Code before loop Code indo-while-block true condition false Code after Flow Control (16) • do-while

  48. Flow Control (17) • do-while <html> <head> <title>Flow Control</title> </head> <body> <?php $sum = 0; $i = 0; do { $i++; $sum += $i; } while ($i < 10); echo "Sum 1 to 10 is $sum"; ?> </body> </html>

  49. Flow Control (18) • break • break ends execution of the current for, foreach, while, do-while or switch structure. • break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. • continue • continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. • continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

  50. Flow Control (19) • break & continue <?php while (condition1) { for (initial; condition2; end) { if (condition3) continue 2; elseif (condition4) { continue; } } if (condition5) { continue; } elseif (condition6) { break; } } ?>

More Related