PHP Kanida Sinmai ksinmai@hotmail.com http://mis.csit.sci.tsu.ac.th/kanida What You Should Already Know • HTML • CSS • JavaScript What is PHP? • • • • PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use What is a PHP File? • PHP files can contain text, HTML, CSS, JavaScript, and PHP code • PHP code are executed on the server, and the result is returned to the browser as plain HTML • PHP files have extension ".php" What Can PHP Do? • • • • • • • PHP can generate dynamic page content PHP can create, open, read, write, delete, and close files on the server PHP can collect form data PHP can send and receive cookies PHP can add, delete, modify data in your database PHP can be used to control user-access PHP can encrypt data Why PHP? • • • • • PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP supports a wide range of databases PHP is free. Download it from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side What Do You Need? To start using PHP, you can: • Find a web host with PHP and MySQL support • Install a web server on your own PC, and then install PHP and MySQL Set Up PHP on Your Own PC • install a web server • install PHP • install a database, such as MySQL Basic PHP Syntax • A PHP script can be placed anywhere in the document. • A PHP script starts with <?php <?php and ends with ?>: // PHP code goes here ?> Example <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html> Comments in PHP // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */ Case Sensitive <?php $color = "red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?> Result? PHP Variables • A variable starts with the $ sign, followed by the name of the variable: <?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?> Rules for PHP variables: • • • • A variable starts with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) • Variable names are case-sensitive ($age and $AGE are two different variables) Output Variables <?php $txt = "W3Schools.com"; echo "I love $txt!"; ?> <?php $txt = "W3Schools.com"; echo "I love " . $txt . "!"; ?> <?php $x = 5; $y = 4; echo $x + $y; ?> The PHP echo Statement Display Text <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?> The PHP echo Statement • Display Variables <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; $x = 5; $y = 4; echo "<h2>$txt1</h2>"; echo "Study PHP at $txt2<br>"; echo $x + $y; ?> PHP Data Types: String <?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?> PHP Integer <?php $x = 5985; var_dump($x); ?> PHP Float <?php $x = 10.365; var_dump($x); ?> PHP Boolean $x = true; $y = false; if (x) print("This will always print<br>"); else print("This will never print<br>"); PHP Array <?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?> Try me! <?php $test_String = "It Worked!”; echo $test_String; ?> จากตัวอย่างซ้ าย ให้ ตอบคาถามต่อไปนี ้ 1. จะเกิดอะไรขึ ้นหากเปลี่ยนจาก Double Quotes เป็ น Single Quotes ‘It Worked!’ 2. จะเกิดอะไรขึ ้นหากข้ างหน้ า เป็ น Single Quotes และข้ างหลังเป็ น Double Quotes ‘It Worked!” 3. จะเกิดอะไรขึ ้นหากลบเครื่ องหมาย $ จากตัวแปร test_String 4. จะเกิดอะไรขึ ้นหากลบเครื่ องหมาย ; ออก 5. จะเกิดอะไรขึ ้นหากเรี ยกใช้ ตวั แปรนี ้ $Test_String PHP : Arithmetic Operators There are five basic arithmetic operators. • + (addition) • - (subtraction) • * (multiplication) • / (division) • % (modulus) Example <?php $x=100; $y=60; echo "The sum of x and y is : ". ($x+$y) ."<br />"; echo "The difference between x and y is : ". ($x-$y) ."<br />"; echo "Multiplication of x and y : ". ($x*$y) ."<br />"; echo "Division of x and y : ". ($x/$y) ."<br />"; echo "Modulus of x and y : " . ($x%$y) ."<br />"; ?> PHP Comparison Operators Operator Name Example == Equal $x == $y === Identical $x === $y != Not equal $x != $y <> Not equal $x <> $y !== Not identical $x !== $y < Less than $x < $y argument). Result TRUE if $x is exactly equal to $y TRUE if $x is exactly equal to $y, and they are of the same type. TRUE if $x is exactly not equal to $y. TRUE if $x is exactly not equal to $y. TRUE if $x is not equal to $y, or they are not of the same type. TRUE if $x (left-hand argument) is strictly less than $y (right-hand > Greater than $x > $y hand argument). TRUE if $x (left hand argument) is strictly greater than $y (right <= Less than or equal to $x <= $y TRUE if $x (left hand argument) is less than or equal to $y (right hand argument). >= Greater than or equal to $x >= $y TRUE if $x is greater than or equal to $y. Result? <?php $x = 300; $y = "300"; var_dump($x == $y); //?? var_dump($x === $y); //?? ?> PHP : Logical Operators Operator && || xor Name and or xor Example $x && $y $x || $y $x xor $y ! and or not and or !$x $x and $y $x or $y Result is true if both $x and $y are true. is true if either $x or $y is true. is true if either $x or $y are true, but not both. is true if $x is not true. is true if both $x and $y are true. is true if either $x or $y is true. PHP String Operators <?php $txt1 = "Hello"; $txt2 = " world!"; echo $txt1 . $txt2; ?> <?php $txt1 = "Hello"; $txt2 = " world!"; $txt1 .= $txt2; echo $txt1; ?> PHP - Decision Making The If...Else Statement <?php $d=date("D"); echo $d; if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> The ElseIf Statement <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> The Switch Statement <?php $d=date("D"); case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; switch ($d) { case "Mon": echo "Today is Monday"; break; case "Sat": echo "Today is Saturday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Sun": echo "Today is Sunday"; break; default: echo "Wonder which day is this ?"; } ?> Example: Switch <?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?> Loop: while while (condition is true) { code to be executed; } <?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> Example <?php $i = 0; $num = 50; while( $i < 10) { $num--; $i++; } echo ("Loop stopped at i = $i and num = $num" ); ?> The do...while loop statement do { code to be executed; } while (condition is true); <html> <body> <?php $i = 0; $num = 0; do{ $i++; } while( $i < 10 ); echo ("Loop stopped at i = $i" ); ?> </body> </html> For Loop for (init counter; test counter; increment counter) { code to be executed; } <?php for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; } ?> Example <?php $a = 0; $b = 0; for( $i=0; $i<5; $i++ ) { $a += 10; $b += 5; } echo ("At the end of the loop a=$a and b=$b" ); ?> The PHP foreach Loop foreach ($array as $value) { code to be executed; } <?php $array = array( 1, 2, 3, 4, 5); foreach( $array as $value ) { echo "Value is $value <br />"; } ?> Exercises • เขียนโปรแกรมขายดอกไม้ โดยคิดราคาเป็ นจานวนดอก ดอกกุหลาบ ดอกละ 25 บาท ดอกกล้ วยไม้ ดอกละ 20 บาท ดอกลิลลี่ ดอกละ 250 บาท โดยกาหนดค่าดอกไม้ และจานวนดอกที่ซื ้อ จากนันค ้ านวณหาราคา รวม (ใช้ Switch statement) Exercises 1. จงเขียนโปรแกรมโดยใช้ For Loop เพื่อคานวณหาผลรวมของตัวเลขตังแต่ ้ 1-30 2. จงเขียนสคริ ปต์เพื่อสร้ าง Pattern ดังนี ้ (for loop) * ** *** **** *** **