Top Banner
1 Introduction PHP ถถถถถถถถถถถถถถถถถถถถ C ถถถถถถถถ Personal Home Page ถถถถ Server-side script language ถถถถ Open Source ถถถถถถ Web application
48

PHP Tutorial (introduction)

Jul 24, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP Tutorial (introduction)

1

Introduction PHP

ถู�กพั�ฒนาขึ้��นด้�วยภาษา C ย�อมาจาก Personal Home Page เป็�น Server-side script language เป็�น Open Source ใช้�ทำ�า Web application

Page 2: PHP Tutorial (introduction)

2

Introduction to Web Technology Web Server คื�อ อะไร Application ทำ#$ใช้�หน�าทำ#$ร �บ และป็ระมวลผล

ขึ้�อม�ลทำ#$ร �องขึ้อ จากผ��ใช้�บร+การอ+นเตอร-เน.ต ผ�านทำางเว.บบราวเซอร- (Browser)

Web Browser คื�อ อะไร Software application ทำ#$ใช้�ร�บและแสด้ง Web pages โด้ยจะทำ�าการ

interpret HTML HTML คื�อ อะไร

Hyper Text Markup Language เป็�นภาษาทำ#$ใช้�สร�าง document บน

World Wide Web เก.บในล�กษณะ ASCII text ป็ระกอบด้�วย markup tags

มากมาย Web Site คื�อ อะไร

คื�อ location บน World Wide Web โด้ยแต�ละ web site จะป็ระกอบด้�วยหน�$ง

home page เป็�น document แรกทำ#$สามารส�$อสารก�บ user และ files หร�อ

documents อ�$น

Page 3: PHP Tutorial (introduction)

3

Introduction to Web Technology HTTP คื�อ อะไร

Hyper Text Transfer Protocol เป็�น application ในระด้�บ protocol TCP/IP ทำ#$ใช้�ทำ�าหน�าทำ#$ deliver virtually all files

and data on the World Wide Web โด้ย

Web Browser and Web Servers สามารถูต+ด้ต�อส�$อสารก�น โด้ยใช้�

HTTP, allows a variety of clients to communicate with and vendor’s server without compatibility

problems

Client sends a request to the server (HTTP request) Server sends a response to the client (HTTP response)

Browser is a HTTP Client (or Web client) เพัราะสามารถูส�ง request มาย�ง HTTP Server ได้�

Page 4: PHP Tutorial (introduction)

4

Introduction to Web Technology

Web Application คื�อ อะไร เพั�$อให�การพั�ฒนา web page จากทำ#$เคืยเป็�นแบบ static

content ด้�วย HTML เพั#ยงอย�างเด้#ยว ป็3จจ4บ�น user ทำ#$ใช้�งาน web page

ต�องการคืวาม สนองตอบทำ#$รวด้เร.ว ทำ�นทำ#$ทำ�นใด้ จ�งได้�ม#การพั�ฒนาคื+ด้คื�นร�ป็

แบบขึ้อง web page ให�เป็�น dynamic มากขึ้��น โด้ยทำ�$วไป็ล�กษณะขึ้อง web

application ก.จะป็ระกอบด้�วย

การใช้� HTML forms ในการส�งขึ้�อม�ลไป็ย�ง Server User สามารถูส�งคื�า parameter หร�อขึ้�อม�ลมาย�ง program ทำ#$

พั�ฒนาขึ้��นส�าหร�บ web site ในฝั่3$ ง server เพั�$อร�บคื�า response ทำ#$ต�องการ เพั�$อตอบสนองการทำ�างานขึ้อง user ให�เป็�นแบบ dynamic

content Other technologies: Java Servlets, ASP, JSP, PHP

Page 5: PHP Tutorial (introduction)

5

Introduction to PHP

อยากใช้� PHP ต�องม#อะไรบ�าง server + web server application + PHP

Apache IIS

client + web browser DBMS

MySQL ORACLE

MS ACCESS

Page 6: PHP Tutorial (introduction)

6

ต+ด้ต��ง Xampp เพั�$อใช้�งาน PHP

Xampp ป็ระกอบด้�วย MySQL root password reset

Apache 2.4.4 PHP 5.4.19

MySQL 5.5.32 phpMyAdmin 4.0.4

Page 7: PHP Tutorial (introduction)

ด้าวน-โหลด้ Xampp ทำ#$ไหน?

• http://www.apachefriends.org

7

Page 8: PHP Tutorial (introduction)

8

Editor Program

NetBeans IDE 7.3.1+ JDK 7u40 with NetBeans 7.3.1

EditPlus NotePad

Page 9: PHP Tutorial (introduction)

9

Introduction to PHP (ต�อ)

PHP Tag<?php …?>

<?...?>

<script language=php>…</script>

จบคื�าส�$งด้�วย ;

Page 10: PHP Tutorial (introduction)

10

Introduction to PHP (ต�อ)

โป็รแกรม PHP ต�วแรก//first.php

<?php echo “Hello world"; ?>

Comment รายบรรทำ�ด้ใช้� // หร�อ Sharp (#)

แบบช้�วงใช้� /*…*/

Page 11: PHP Tutorial (introduction)

11

ต�วแป็รและต�วด้�าเน+นการ โด้ยทำ�$วไป็การใช้�ต�วแป็ร PHP ไม�จ�าเป็�นต�องระบ4ช้น+ด้

(Type) การต��งช้�$อต�วแป็รใช้�ต�วอ�กษร ภาษาอ�งกฤษ, 0-9, _ โด้ยจะ

ต�อง ขึ้��นต�นด้�วย ต�วอ�กษรหร�อ _ เทำ�าน��น และใช้� $ น�าหน�าช้�$อ

ต�วแป็ร เราสามารถูอ�างถู�งคื�าในต�วแป็ร โด้ยเร#ยกผ�านต�วแป็รอ�$น

(assign by reference)

Demo <?php //ref.php

$name = “Tum";$money = 100;echo “name = “ . $name ;echo “money = ” . $money ;

?>

Page 12: PHP Tutorial (introduction)

12

ต�วแป็รและต�วด้�าเน+นการช้น+ด้ขึ้�อม�ลต�วแป็ร

boonlean integer float string array object

Page 13: PHP Tutorial (introduction)

13

ต�วแป็รและต�วด้�าเน+นการsettype($varname, “integer”); ช้น+ด้ขึ้�อม�ลต�วแป็ร

boonlean integer float string array object

Page 14: PHP Tutorial (introduction)

14

ต�วแป็รและต�วด้�าเน+นการ การต��งช้�$อต�วแป็รเป็�น case sensitive (เล.กก.เล.กใหญ่�ก.

ใหญ่�)Demo

<? //var01.php$num = 12.535;echo $num;

?><? //var02.php

$num = (integer)12.535;echo $num;

?>

Page 15: PHP Tutorial (introduction)

15

ต�วแป็รและต�วด้�าเน+นการDemo

<? //var03.php$num = 12.535;settype($a_num, "integer");echo $num;

?><? //var04.php

$num = (integer)12.535;$num = (float)($num * 1.23);echo $num;echo "<br>";echo $num;

?>

Page 16: PHP Tutorial (introduction)

16

ต�วแป็รและต�วด้�าเน+นการ String Operators

ใช้�เช้�$อม string 2 ช้4ด้เขึ้�าด้�วยก�น. จ4ด้

Page 17: PHP Tutorial (introduction)

17

ต�วแป็รและต�วด้�าเน+นการ ต�วแป็รแบบ Local

เป็�นต�วแป็รทำ#$ถู�กสร�างขึ้��นภายใน function ใช้�งานได้�เฉพัาะภายใน function

ไม�สามารถูเร#ยกใช้�งานภายนอก function ได้� Demo

<? //var05.phpfunction ShowProvince()

{echo "My province : ".$myprovince;

} $myprovince = "Bangkok";

ShowProvince();?>

Page 18: PHP Tutorial (introduction)

18

ต�วแป็รและต�วด้�าเน+นการ ต�วแป็รแบบ global

หากต�องการเร#ยกใช้�เป็�นต�วแป็รทำ#$ถู�กสร�างขึ้��นภายใน function ต�องป็ระกาศต�วแป็ร global เพั�$อให�สามารถู

เร#ยกใช้�งานภายนอก function ได้� Demo

<? //var06.php

function ShowProvince() {

global $myprovince;echo "My province : ".$myprovince;

} $myprovince = "Bangkok";

ShowProvince();?>

Page 19: PHP Tutorial (introduction)

19

ต�วแป็รและต�วด้�าเน+นการ ช้น+ด้ขึ้�อม�ล

boolean integer float string array object

resource

Page 20: PHP Tutorial (introduction)

20

ต�วแป็รและต�วด้�าเน+นการ boolean

True / false

คื�าขึ้องต�วแป็รช้น+ด้อ�$นสามารถูแป็ลงมาเป็�นผลล�พัธ์-แบบ boolean ได้�โด้ยอ�ตโนม�ต+โด้ยคื�าทำ#$สามารถูแป็ลคืวามหมาย

เป็�น false ได้�แก�Data Type คื�าขึ้องขึ้�อม�ล

Integer 0 หร�อ NULL

Float 0.0 หร�อ NULL

String “” หร�อ “0” หร�อ NULL

Array ทำ#$ไม�ม#คื�าใด้ๆ อย�� $a = array() หร�อ NULL

Object ทำ#$ไม�ม#คื�าใด้ๆ อย�� หร�อ NULL

Page 21: PHP Tutorial (introduction)

21

ต�วแป็รและต�วด้�าเน+นการDemo

<? //boolean.php$i = 0;if($i) {

echo "$i <> 0"; }else {

echo "$i = 0”; }

?>

Page 22: PHP Tutorial (introduction)

22

ต�วแป็รและต�วด้�าเน+นการ integerเลขึ้จ�านวนเต.ม

สามารถูใส�คื�าเป็�นเลขึ้ฐานส+บ ฐานแป็ด้ หร�อฐานส+บหกได้� โด้ย ใส�เลขึ้ศ�นย- (0) เม�$อต�องการแสด้งเลขึ้ฐานแป็ด้

ใส� (0x) เพั�$อแสด้งว�าเป็�นเลขึ้ฐานส+บหก

Demo

<? #integer.php

$x = 10; echo "\$x = ".$x."<br>";

$y = 010; echo "\$y = ".$y."<br>";

$z = 0x10; echo "\$z = ".$z."<br>";

?>

Page 23: PHP Tutorial (introduction)

23

ต�วแป็รและต�วด้�าเน+นการDemo

<? /* var07.php

$x = "555";echo "\$x is $x : ".gettype($x)."<br>";$x = $x + 0;echo "\$x is $x : ".gettype($x)."<br>";

?>

Page 24: PHP Tutorial (introduction)

24

ต�วแป็รและต�วด้�าเน+นการ float

ต�วเลขึ้ทำ#$ม#จ4ด้ทำศน+ยม string

ขึ้�อคืวาม กล4�มขึ้องต�วอ�กษร object

เป็�น instance ขึ้อง class ส�าหร�บการเขึ้#ยนโป็รแกรมแบบ OOP

resource ขึ้�อม�ลช้น+ด้พั+เศษใช้�อ�างถู�งทำร�พัยากรภายนอก ซ�$งฟั3งก-พั+เศษ

บางฟั3งก-ช้�นจะคื�นคื�าเป็�น ขึ้�อม�ลช้น+ด้น#�กล�บมา เช้�น mysql_connect()

Page 25: PHP Tutorial (introduction)

25

ต�วแป็รและต�วด้�าเน+นการ Arithmetic Operators

+ - * / % Demo

<? /* arop01.php */$a = 10;$b = -3;echo "\$a + \$b = ".($a+$b)."<br>";echo "\$a / \$b = ".$a/$b."<br>";echo "\$a % \$b = ".$a%$b."<br>";//echo "\$a % \$b = ".$a+$b."<br>";

?>

Page 26: PHP Tutorial (introduction)

26

ต�วแป็รและต�วด้�าเน+นการ Assignment Operators

= += -= .= Demo

<? /* asop01.php */$a = $b = 15;$a -= 5;$b += 3;echo "\$a = ".$a."<br>";echo "\$b = ".$b."<br>";$a .= $b;echo "\$a .= ".$a."<br>";$a *= $b;echo "\$a *= ".$a."<br>";

?>

Page 27: PHP Tutorial (introduction)

27

ต�วแป็รและต�วด้�าเน+นการ Comparision Operators

ใช้�เป็ร#ยบเทำ#ยบระหว�างสองคื�า == != <>

=== !==

< >

<= >=

Page 28: PHP Tutorial (introduction)

28

ต�วแป็รและต�วด้�าเน+นการ Demo

<? /* coop01.php */

$a = 15;

$b = "15";

if($a == $b) {echo "\$a == \$b<br>";}

else{echo "\$a != \$b<br>";}

if($a === $b) {echo "\$a === \$b<br>";}

else{print "\$a !== \$b<br>";}

?>

Page 29: PHP Tutorial (introduction)

29

ต�วแป็รและต�วด้�าเน+นการ Operator แบบม#เง�$อนไขึ้

condition ? Value if true : value if false; Demo

<? #condition.php

$a = 1000;

$x = isset($b)? $b : $a;

echo "\$x = $x";

?>

Page 30: PHP Tutorial (introduction)

30

ต�วแป็รและต�วด้�าเน+นการ Error Control Operator

ใช้�ส�าหร�บคืวบคื4มขึ้�อผ+ด้พัลาด้ โด้ยใช้� @ น�าหน�า expression ทำ#$ต�องการด้�กขึ้�อผ+ด้พัลาด้

หากม#ขึ้�อผ+ด้พัลาด้เก+ด้ขึ้��น จะไม�ม#การแสด้งผลขึ้�อผ+ด้พัลาด้ทำ#$ interprete พับ

Demo

<? #eco.php$filename = "myfile.php";$filefound = @fopen($filename, "r");if($filefound){

echo "found $filename<br>";}else{

echo "did not find $filename<br>";}

?>

Page 31: PHP Tutorial (introduction)

31

ต�วแป็รและต�วด้�าเน+นการ incrementing/Decrementing Operators

ใช้�ส�าหร�บเพั+$มลด้คื�าต�วแป็ร ++$i $i++ --$i $i--

Demo

<? #ido.php$i = 0;echo $i++;echo $i;

?>

Page 32: PHP Tutorial (introduction)

32

ต�วแป็รและต�วด้�าเน+นการ Logical Operators

ใช้�ส�าหร�บเป็ร#ยบเทำ#ยบคื�าต�วแป็รหร�อน+พัจน- and && …AND

or || OR

xor …EXCLUSIVE OR

! …NOT

Page 33: PHP Tutorial (introduction)

33

ต�วแป็รและต�วด้�าเน+นการ if()…else, if()…elseif

ใช้�ตรวจสอบเง�$อนไขึ้เพั�$อเล�อกด้�าเน+นการอย�างใด้อย�างหน�$ง Demo

<? #ifelse.php$i = 0;$a = $i++;if($a===$i){

echo ($a."<br>");}else{

echo ($i."<br>".$a."<br>");}

?>

Page 34: PHP Tutorial (introduction)

34

คื�าส�$งในการคืวบคื4ม Demo

<? #ifendif.php

$i = 0;

$a = $i++;

if($a===$i):

echo ($a."<br>");

else:

echo ($i."<br>".$a."<br>");

endif

?>

Page 35: PHP Tutorial (introduction)

35

คื�าส�$งในการคืวบคื4ม Demo

<? #ifelseif.php$i = 0;//$a = 0;$a = $i++;if($a>$i){

echo ($a."<br>");}elseif($a<$i){

echo ($i."<br>");}else{

echo($i."<br>".$a."<br>");}

?>

Page 36: PHP Tutorial (introduction)

36

คื�าส�$งในการคืวบคื4ม switch()…case…break

ใช้�เม�$อต�องการเป็ร#ยบเทำ#ยบคื�าต�างๆ เพั�$อหาทำางเล�อกจากหลายๆ ทำางเล�อกว�าจะให�ด้�าเน+นการตาม

ทำางเล�อกใด้ทำางเล�อกหน�$ง Demo

<? #switch.php$i = 0;$a = $i++;switch($a){

default : print " i = 0 <br>"; break;case 0: print " a = 0 <br>"; break;case 1: print " a = 1 <br>";break;

} ?>

Page 37: PHP Tutorial (introduction)

37

คื�าส�$งในการคืวบคื4ม while()

การทำ�างานแบบวนรอบจนกว�าเง�$อนไขึ้ขึ้อง while จะเป็�นเทำ.จ

Demo

<? #while.php

$i = 0;

while($i<10){

print ++$i."<br>";

}

?>

Page 38: PHP Tutorial (introduction)

38

คื�าส�$งในการคืวบคื4ม do…while()

การทำ�างานแบบวนรอบจนกว�าเง�$อนไขึ้ขึ้อง while จะเป็�นเทำ.จ โด้ยในรอบแรกจะถู�กป็ระมวลผลก�อนทำ�าการตรวจสอบ

เง�$อนไขึ้ Demo

<? #dowhile.php

$i = 0;

do{

print ++$i."<br>";

}while($i<10)

?>

Page 39: PHP Tutorial (introduction)

39

คื�าส�$งในการคืวบคื4ม for()

การทำ�างานแบบวนรอบตามจ�านวนคืร��งทำ#$ทำราบแน�นอน และม#การเพั+$มคื�า

ให�ก�บต�วแป็รทำ#$ใช้�ตรวจสอบเง�$อนไขึ้ในอ�ตราทำ#$สม�$าเสมอ Demo

<? #forloop01.php

$i = 0;

for($i=1; $i<=10; $i++){

print $i."<br>";

}

?>

Page 40: PHP Tutorial (introduction)

40

คื�าส�$งในการคืวบคื4ม for()

Demo

<? #forloop02.php

$i = 0;

for(; $i++<10;){

print $i."<br>";

}

?>

Page 41: PHP Tutorial (introduction)

41

คื�าส�$งในการคืวบคื4ม break()

ใช้�เพั�$อออกจากการทำ�างานแบบวน loop ขึ้องคื�าส�$งคืวบคื4ม for, foreach,

while, do…while และ switch Demo

<? #break.php$i = 1;for(;;){

print $i."<br>";if($i++>9){

break;}}

?>

Page 42: PHP Tutorial (introduction)

42

คื�าส�$งในการคืวบคื4ม continue()

ใช้�ในกรณ#เม�$อต�องการให�ม#การขึ้�ามการทำ�างานบางอย�างภายใน loop ไป็ แล�วกล�บ

ไป็ตรวจสอบเง�$อนไขึ้ขึ้องคื�าส�$งคืวบคื4ม for, while, do…while อ#กคืร��ง Demo

<? #continue.php$i = 0;while(++$i<10){

print "\$i=$i -->".($i % 3)."<br>";if($i % 3){

continue;}print $i."<br>";

} ?>

Page 43: PHP Tutorial (introduction)

43

คื�าส�$งในการคืวบคื4ม return()

ใช้�ในฟั3งก-ช้� $นทำ#$ต�องการส�งคื�ากล�บออกมานอกฟั3งก-ช้� $น Demo

<? #return.phpfunction TestSet($a){

echo ("\$a is $a");return ++$a;

}$iTestSet = "TestSet";$i=$iTestSet(5);print "<br>".$i;

?>

Page 44: PHP Tutorial (introduction)

44

คื�าส�$งในการคืวบคื4ม require(), include()

ใช้�เม�$อต�องการแทำรก script จากไฟัล-หน�$งเขึ้�าในอ#กไฟัล-หน�$ง โด้ยจะ

ให�ผลล�พัธ์-เหม�อนก�บว�า script ทำ#$แทำรกเขึ้�ามาถู�กพั+มพั-อย��ในไฟัล-

น��นโด้ยตรง เหม�อนก�บ require(), include() จะป็>องก�นไม�ให�ม#การแทำรกไฟัล-ซ��า หากไฟัล-น��นเคืยถู�กแทำรกมาแล�วใน

script

น#�คืร��งหน�$ง

Page 45: PHP Tutorial (introduction)

45

คื�าส�$งในการคืวบคื4ม require(), include()

Demo

<? #MyFunction.php

function TestSet($a){

print("\$a is $a");

return ++$a;

}

?>

Page 46: PHP Tutorial (introduction)

46

คื�าส�$งในการคืวบคื4ม require(), include()

ใช้�เม�$อต�องการแทำรก script จากไฟัล-หน�$งเขึ้�าในอ#กไฟัล-หน�$ง โด้ยจะให�ผลล�พัธ์-

เหม�อนก�บว�า script ทำ#$แทำรกเขึ้�ามาถู�กพั+มพั-อย��ในไฟัล-น��นโด้ยตรง

Demo

<? #include.php

include("MyFunction.php");

$iTestSet = "TestSet";

$i=$iTestSet(5);

print "<br>".$i;

?>

Page 47: PHP Tutorial (introduction)

47

เทำคืน+คืการเขึ้#ยนโป็รแกรมใน Netbean เป็?ด้แทำ.กจะต�องป็?ด้แทำ.กทำ4กคืร��ง เช้�น

<?php … ?><html>….</html>

คืวรเขึ้#ยนแทำ.กให�คืรบก�อนทำ4กคืร��งแล�วจ�งเขึ้#ยนคื�าส�$งแทำรกลงภายในแทำ.กทำ#หล�ง

ให�ส�งเกตส#ขึ้ององคื-ป็ระกอบภายในโป็รแกรมจะม#ส#ทำ#$แตกต�างก�น เช้�น

แทำ.กขึ้อง PHP ส#น��าตาล<?...?>

<?php…?> แทำ.กขึ้อง HTML ส#น��าเง+น

<html><head></head></html>

Page 48: PHP Tutorial (introduction)

48

เทำคืน+คืการเขึ้#ยนโป็รแกรมใน EditPlus หมายเหต4 ส#เขึ้#ยว เช้�น

//comment#comment

ขึ้�อม�ลภายใน “…” ส#ช้มพั�“\$a = $a<br>”

ฟั3งก-ช้�$น ส#ส�ม เช้�นecho “…”print “…”

คื�าส�$ง (statement) ส#น��าเง+น เช้�นswitch($selSex)

ต�วแป็ร (variable) จะต�องม# $ และส#เขึ้#ยว เช้�น$selSex

วงเล.บ ส#น��าตาล(…..)