ดาวน์โหลดงานนำเสนอ
งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ
ได้พิมพ์โดยประมนต์ บราวน์ ได้เปลี่ยน 9 ปีที่แล้ว
1
PHP
2
What You Should Already Know HTML CSS JavaScript
3
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
4
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"
5
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
6
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.netwww.php.net PHP is easy to learn and runs efficiently on the server side
7
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
8
Set Up PHP on Your Own PC install a web server install PHP install a database, such as MySQL
9
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 ?>
10
Example My first PHP page <?php echo "Hello World!"; ?>
11
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 */
12
Case Sensitive <?php $color = "red"; echo "My car is ". $color. " "; echo "My house is ". $COLOR. " "; echo "My boat is ". $coLOR. " "; ?> Result?
13
PHP Variables A variable starts with the $ sign, followed by the name of the variable: <?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?>
14
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)
15
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; ?>
16
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."; ?>
17
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; ?>
18
PHP Data Types: String <?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo " "; echo $y; ?>
19
PHP Integer <?php $x = 5985; var_dump($x); ?>
20
PHP Float <?php $x = 10.365; var_dump($x); ?>
21
PHP Boolean $x = true; $y = false; if (x) print("This will always print "); else print("This will never print ");
22
PHP Array <?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?>
23
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
24
PHP : Arithmetic Operators There are five basic arithmetic operators. + (addition) - (subtraction) * (multiplication) / (division) % (modulus)
25
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)." "; ?>
26
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.
27
Result? <?php $x = 300; $y = "300"; var_dump($x == $y); //?? var_dump($x === $y); //?? ?>
28
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.
29
PHP String Operators <?php $txt1 = "Hello"; $txt2 = " world!"; echo $txt1. $txt2; ?> <?php $txt1 = "Hello"; $txt2 = " world!"; $txt1.= $txt2; echo $txt1; ?>
30
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!"; ?>
31
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!"; ?>
32
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 ?"; } ?>
33
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!"; } ?>
34
Loop: while while (condition is true) { code to be executed; } <?php $x = 1; while($x <= 5) { echo "The number is: $x "; $x++; } ?>
35
Example <?php $i = 0; $num = 50; while( $i < 10) { $num--; $i++; } echo ("Loop stopped at i = $i and num = $num" ); ?>
36
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);
37
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 "; } ?>
38
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" ); ?>
39
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 "; } ?>
40
Exercises เขียนโปรแกรมขายดอกไม้ โดยคิดราคาเป็น จำนวนดอก ดอกกุหลาบ ดอกละ 25 บาท ดอกกล้วยไม้ ดอกละ 20 บาท ดอกลิลลี่ ดอกละ 250 บาท โดยกำหนดค่าดอกไม้ และจำนวนดอกที่ซื้อ จากนั้นคำนวณหาราคารวม ( ใช้ Switch statement)
41
Exercises 1. จงเขียนโปรแกรมโดยใช้ For Loop เพื่อคำนวณหาผลรวมของ ตัวเลขตั้งแต่ 1-30 2. จงเขียนสคริปต์เพื่อสร้าง Pattern ดังนี้ (for loop) * * * * * * * * * * *
งานนำเสนอที่คล้ายกัน
© 2024 SlidePlayer.in.th Inc.
All rights reserved.