PHP
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: 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 and ends with ?>: <?php // PHP code goes here ?>
Example My first PHP page <?php echo "Hello World!"; ?>
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. " "; echo "My house is ". $COLOR. " "; echo "My boat is ". $coLOR. " "; ?> 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 " PHP is Fun! "; echo "Hello world! "; echo "I'm about to learn PHP! "; 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 " $txt1 "; echo "Study PHP at $txt2 "; echo $x + $y; ?>
PHP Data Types: String <?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo " "; echo $y; ?>
PHP Integer <?php $x = 5985; var_dump($x); ?>
PHP Float <?php $x = ; var_dump($x); ?>
PHP Boolean $x = true; $y = false; if (x) print("This will always print "); else print("This will never print ");
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)." "; echo "The difference between x and y is : ". ($x-$y)." "; echo "Multiplication of x and y : ". ($x*$y)." "; echo "Division of x and y : ". ($x/$y)." "; echo "Modulus of x and y : ". ($x%$y)." "; ?>
PHP Comparison Operators OperatorNameExampleResult = =Equal$x == $yTRUE if $x is exactly equal to $y = = =Identical$x === $yTRUE if $x is exactly equal to $y, and they are of the same type. !=Not equal$x != $yTRUE if $x is exactly not equal to $y. <>Not equal$x <> $yTRUE if $x is exactly not equal to $y. !==Not identical $x !== $yTRUE if $x is not equal to $y, or they are not of the same type. <Less than$x < $yTRUE if $x (left-hand argument) is strictly less than $y (right-hand argument). >Greater than $x > $yTRUE if $x (left hand argument) is strictly greater than $y (right hand argument). <=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 >= $yTRUE 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 OperatorNameExampleResult &&and$x && $yis true if both $x and $y are true. ||or$x || $yis true if either $x or $y is true. xorxor$x xor $yis true if either $x or $y are true, but not both. !not!$xis true if $x is not true. andand$x and $yis true if both $x and $y are true. oror$x or $yis 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"); switch ($d) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; break; case "Thu": echo "Today is Thursday"; break; case "Fri": echo "Today is Friday"; break; case "Sat": echo "Today is Saturday"; 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 "; $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 <?php $i = 0; $num = 0; do{ $i++; } while( $i < 10 ); echo ("Loop stopped at i = $i" ); ?> do { code to be executed; } while (condition is true);
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 "; } ?>
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 "; } ?>
Exercises เขียนโปรแกรมขายดอกไม้ โดยคิดราคาเป็น จำนวนดอก ดอกกุหลาบ ดอกละ 25 บาท ดอกกล้วยไม้ ดอกละ 20 บาท ดอกลิลลี่ ดอกละ 250 บาท โดยกำหนดค่าดอกไม้ และจำนวนดอกที่ซื้อ จากนั้นคำนวณหาราคารวม ( ใช้ Switch statement)
Exercises 1. จงเขียนโปรแกรมโดยใช้ For Loop เพื่อคำนวณหาผลรวมของ ตัวเลขตั้งแต่ จงเขียนสคริปต์เพื่อสร้าง Pattern ดังนี้ (for loop) * * * * * * * * * * *