Professional Home Page : PHP

58
Professional Home Pa Professional Home Pa ge : ge : PHP PHP เเเเ เเเเ เเเเ เเเเ เเ เเ [email protected] [email protected]

description

Professional Home Page : PHP. เสรี ชิโนดม [email protected]. เนื้อหา. ประวัติของ PHP PHP คืออะไร ทำไม PHP จึงเป็นที่นิยม โครงสร้างของ PHP Language Reference ข้อแตกต่างของ PHP กับ ASP. ประวัติ PHP. PHP ย่อมาจาก Professional Home Page เริ่มสร้างขึ้นในกลางปี 1994 - PowerPoint PPT Presentation

Transcript of Professional Home Page : PHP

Page 1: Professional Home Page  : PHP

Professional Home Page Professional Home Page::PHP PHP

เสรี� ชิ�โนดม เสรี� ชิ�โนดม[email protected]@buu.ac.th

Page 2: Professional Home Page  : PHP

เน��อหาเน��อหา

ประวั�ติ�ของ PHPPHP คื ออะไร ทำ��ไม PHP จึ�งเป�นทำ��น�ยม โคืรงสร��งของ PHPLanguage Reference ข�อแติกติ �งของ PHP ก�บ ASP

Page 3: Professional Home Page  : PHP

ปรีะวั�ติ� PHPPHP ย อม�จึ�ก Professional Home Page เร��มสร��งข�"นในกล�งป% 1994

ผู้'�พั�ฒน�คื อ น�ย Rasmus Lerdorf ป*จึจึ+บ�น PHP ม�ก�รพั�ฒน�ม�เป�นร+ นทำ�� 4

- Version แรกเป�นทำ��ร' �จึ�กในชื่ �อวั � Personal Homepage Tools ในป% 1994 ถึ�งกล�งป%1995- Version ทำ��สองชื่ �อวั � PHP/FI ในกล�งป%1995

Page 4: Professional Home Page  : PHP

ปรีะวั�ติ� PHP (ติ�อ)

- 3Version เป�นทำ��ร' �จึ�กก�นในชื่ �อวั � PHP3 เร��มใชื่� กล�งป%1997

- ป*จึจึ+บ�น BBBB B 4 2 ใชื่�ชื่ �อวั � Zend (Zend ย อม�จึ�ก Ze(ev) + (A)nd(I Gutmans)

Page 5: Professional Home Page  : PHP

รีายชิ��อของน�กพั�ฒนาภาษา PHP Zeev Suraski, Israel Andi Gutmans, Israel Shane Caraveo, Florida USA Stig Bakken, Norway Andrey Zmievski, Nebraska USA Sascha Schumann, Dortmund, Germany Thies C. Arntzen, Hamburg, Germany Jim Winstead, Los Angeles, USA Rasmus Lerdorf, North Carolina, USA

Page 6: Professional Home Page  : PHP

PHP คื�ออะไรี เป�นภ�ษ� Script ส��หร�บแสดงเวั4บเพัจึอย �ง

หน��ง จึ�ดอย' ในกล+ ม Server Side Script เชื่ น เด�ยวัก�บ ASP

ก�รทำ��ง�นจึะแทำรกอย' ในเอกส�ร HTML ส�ม�รถึ Compile ได�ทำ� "งบนระบบปฏิ�บ�ติ�ก�ร

UNIX, Windows NT, Windows 9xคืวั�มส�ม�รถึ ในก�รทำ��ง�นส'ง โดยเฉพั�ะก�บก�ร

ติ�ดติ อก�บ Database เชื่ น MySQL, mSQL, Sybase และ PostgreSQL เป�นติ�น

Page 7: Professional Home Page  : PHP

ส��งที่�� PHP สามารีถที่$าได%%CGIDatabase-enable web page Database

Adabas D InterBase Solid

DBase mSQL Sybase Empress MySQL Velocis

FilePro Oracle Unix dbmInformix PostgreSQL

Page 8: Professional Home Page  : PHP

ที่$าไม PHP จึ'งเป(นที่��น�ยมOpen source No cost implementation – PHP

เป�นของฟร� Server side Open SourceBBBBBBBBB BBBBBBBBHTML embedded Simple language

Page 9: Professional Home Page  : PHP

ที่$าไม PHP จึ'งเป(นที่��น�ยมEfficiency – XML parsing Server side Database module File I/O Text processing Image processing

Page 10: Professional Home Page  : PHP

การีที่$างานของ PHP

ทำ��ง�นบน Server ทำ��ง�นร วัมก�บเอกส�ร htmlส�ม�รถึ แทำรกคื��ส��ง PHP ได�ติ�มทำ��ติ�องก�รลงใน

เอกส�ร html ทำ��ง�นในส วันทำ��เป�นคื��ส��งของ PHP ก อน เม �อม�

ก�รเร�ยกใชื่�เอกส�รน�"น ๆ แสดงผู้ลออกทำ�ง Web Browsers

Page 11: Professional Home Page  : PHP

โคืรีงสรี%างภาษา PHP

แบบทำ�� 1 BB B BBBBB<?php คื��ส�� งภ�ษ� PHP ?>

ติ�วัอย �ง<?php

echo “Hello ! World ! <br>”;

echo “I am PHP”;?>

Page 12: Professional Home Page  : PHP

โคืรีงสรี%างภาษา PHP (ติ�อ)

แบบทำ�� BB B B BBBBB2<? คื��ส��งภ�ษ� PHP ?>ติ�วัอย � ง

<? echo “Hello ! World !

<br>”;echo “I am PHP”;

?>

Page 13: Professional Home Page  : PHP

โคืรีงสรี%างภาษา โคืรีงสรี%างภาษา PHP PHP ((ติ�อติ�อ))

แบบทำ�� 3Java Language style<script language=“php”> คื��ส��งภ�ษ� PHP </script>ติ�วัอย �ง

<script language=“php”>echo “Hello ! World”;

</script>

Page 14: Professional Home Page  : PHP

โคืรีงสรี%างภาษา PHP (ติ�อ)

แบบทำ�� 4 ASP Style<% คื��ส��งภ�ษ� PHP %>ติ�วัอย �ง

<% echo “Hello ! World !

<br>”;echo “I am PHP”;

%>

Page 15: Professional Home Page  : PHP

โคืรีงสรี%างของภาษา PHP (ติ�อ) จึ�กติ�วัอย �ง แบบทำ��เป�นทำ��น�ยม คื อ แบบทำ�� 1ผู้ลทำ��ได�เม �อผู้ �นก�รทำ��ง�นแล�วัจึะได�ผู้ลด�งน�"

Hello ! World ! I am PHP

ข�อส�งเกติ- ร'ปแบบคืล��ยก�บภ�ษ� C และ Perl- ใชื่�เคืร �องหม�ย ( ; ) คื��นระหวั �งคื��ส��งแติ ละคื��ส��ง

File ทำ��ได�ติ�อง save เป�นน�มสก+ล php หร อ php3 เทำ �น�"น

Page 16: Professional Home Page  : PHP

Language ReferenceLanguage ReferenceComments

- เหม อนก�บก�ร Comment ของภ�ษ� C, C++ และ Unixติ�วัอย �ง

<?phpecho “Hello !”; // ก�ร comment แบบ 1 บรรทำ�ด

*/ แบบหล�ยบรรทำ�ด ติ�"งแติ 2 บรรทำ�ดข�"นไป*/

#echo “World”; ก�ร comment แบบ shell-style?>

Page 17: Professional Home Page  : PHP

คื$าส��ง echo

เป�นคื��ส��งส��หร�บแสดงผู้ลล�พัธ์:ไปทำ��โปรแกรม browser

ร'ปแบบของคื��ส��งecho ข�อคืวั�ม 1 หร อติ�วัแปร1 , ข�อคืวั�ม2 หร อติ�วัแปร2, ข�อคืวั�ม3 หร อติ�วัแปร3, …

ข�อคืวั�ม เข�ยนภ�ยใติ�เคืร �องหม�ย double quote (“ “) หร อ single quote (‘ ‘)

ติ�วัแปรของภ�ษ� PHP จึะข�"นติ�นด�วัยเคืร �องหม�ย $ เสมอ คืล��ยก�บภ�ษ� Perl

Page 18: Professional Home Page  : PHP

ติ�วัอย�างที่�� 1 intro-1.php3 <HTML><HEAD><TITLE>Example –1</TITLE>B BB B BB<? echo "Hi, I'm a PHP script!"; ?>B BB B BB</HTML>

Hi, I'm a PHP script!

Page 19: Professional Home Page  : PHP

ติ�วัอย�างที่�� 2 intro-2.php3 <HTML><HEAD><TITLE> Example –2</TITLE></HEAD><BODY>

Today’s Date: <? print(Date("l F d, Y"));?>

</BODY></HTML>

06 2000Today's Date: Thursday April ,

Page 20: Professional Home Page  : PHP

ติ�วัอย�างที่�� 3 intro-3.php3<HTML><HEAD><TITLE> Example –3</TITLE></HEAD><BODY>

Today’s Date:<?*/

BBBBB BBBBBBB BBBB**

*/ print(Date("l F d, Y"));

?></BODY></HTML>

06 2000Today's Date: Thursday April ,

Page 21: Professional Home Page  : PHP

ติ�วัอย�างที่�� 4 <?

B= " Seree"; $Today = date("l F d, Y");

$CostOfLunch = 3.50; $DaysBuyingLunch = 4;

BB<HTML>

<HEAD> <TITLE> Example –4</TITLE>

</HEAD>B BB B BB

’ :<?/*

BBBBBBB BBBB* * ’*/

3 3print("<H >$Today</H >\n");

Page 22: Professional Home Page  : PHP

ติ�วัอย�างที่�� 4 (ติ�อ)/*

** print message about lunch cost*/

print("$YourName, you will be out "); *print($CostOfLunch

$DaysBuyingLunch); print(" dollars this week.<BR>\n");

BBB BBB B BB</HTML>

Page 23: Professional Home Page  : PHP

ติ�วัอย�างที่�� 5 intro-5.php3

<? echo '<pre>First Paragraph:

- CorporalCar r ot , Ankh Mor por k Ci t y Guar d (Night Watch), sat down in his nightshirt,

took up his pencil, sucked the end for a moment, and then wrote:</pre>';

?>

Page 24: Professional Home Page  : PHP

BBBBBBBBBB:

- Corporal Carrot, Ankh Morpork CityGuard

(Night Watch), sat down in hisnightshirt,

took up his pencil, sucked the end for a moment, and then wrote:

Page 25: Professional Home Page  : PHP

Data Types ชื่น�ดของข�อม'ลใน PHP ม�ด�งติ อไปน�"

- Interger : ข�อม'ลชื่น�ดเลขจึ��นวันเติ4ม เชื่ น 1 , 10, 100, 123 เป�นติ�น floating-point numbers : ข�อม'ลชื่น�ดเลขทำศน�ยม เชื่ น 110. , 1.2e-10 เป�นติ�น

Single- quoted String : ข�อม'ลติ�วัอ�กษรเข�ยนภ�ยใติ�เคืร �องหม�ย single quote

Double-quoted String คื อ: ชื่+ดข�อม'ลติ�วัอ�กษรเข�ยนภ�ยใติ�เคืร �องหม�ย double quote คืล��ยก�บ single-quoted string แติ ม�คืวั�มซั�บซั�อนกวั �โดยเร�ส�ม�รถึใชื่�เคืร �องหม�ย \ เพั �อเพั��ม escape sequence หร อ meta characters ในสติร�งก:

Back- quoted String : ก�รใชื่� back-quoted strings ใน PHP ไม ใชื่ ก�รก��หนดสติร�งก: แติ เป�นก�รเร�ยกใชื่�คื��ส��งของระบบ (system command)

Page 26: Professional Home Page  : PHP

ติ�วัอย�างที่�� 6 intro-6.php3

<? echo '<pre>'; echo `ls *.php3`; echo '</pre>';?>

Page 27: Professional Home Page  : PHP

<HTML><HEAD>

<TITLE>Figure 1–4</TITLE></HEAD><BODY>

<FORM ACTION="1–5.php3"METHOD="post">

Your Name: <INPUT TYPE="text"

NAME="YourName"><BR> Cost of a Lunch:

<INPUT TYPE="text"NAME="CostOfLunch"><BR>

Days Buying Lunch: <INPUT TYPE="text"

NAME="DaysBuyingLunch"><BR> <INPUT TYPE="submit">

</FORM></BODY></HTML>

ติ�วัอย�าง

Page 28: Professional Home Page  : PHP

Arrays ม�อย' 2 ชื่น�ด คื อ แบบ indexed arrays และ แบบ

associative arrays ม�ทำ�"ง array แบบ 1 ม�ติ� และ 2 ม�ติ� ติ�วัอย �ง indexed arrays 2 ม�ติ�

$a[0] = “A”; // แบบ 1 ม�ติ�$a[0][1] = “A,A”; // แบบ 2 ม�ติ�

ติ�วัอย �ง associative arrays$a[“time”] = “15.55”;$a[“date”][“time”] = “Wed , 15.55”;

Page 29: Professional Home Page  : PHP

ติ�วัอย�าง -15php3<?

$Today = date("l F d, Y");BB<HTML><HEAD>

1 5

</HEAD>B BB B BB

’ :

Page 30: Professional Home Page  : PHP

<?/*

BBBBBBB BBBB* * ’*/

3 3print("<H >$Today</H >\n");/*

** print message about lunch cost*/

print("$YourName, you will be out "); *print($CostOfLunch

$DaysBuyingLunch); print(" dollars this week.<BR>\n");

BBB BBB B BB</HTML>

Page 31: Professional Home Page  : PHP

<HTML> <HEAD> <TITLE>Figure 1–6</TITLE> </HEAD>

<BODY><H1><?*/

BBB BBBBBBB BBB BB BBB B BBB**

*/ = ( "");

( == "") { (" ’ ! " );

} B{

print("Today is $Today.");}?></H1> </BODY></HTML>

Page 32: Professional Home Page  : PHP

<HTML><HEAD><TITLE>Example–7</TITLE></HEAD><BODY>

<H1>Today’s Daily Affirmation</H1> Repeat three times:<BR>

<? for($count = 1; $count <= 3; $count++)

{ print("<B>$count</B> I’m good enough,

"); print("I’m smart enough, ");

print("and, doggone it, people like me!<BR>\n");

}?></BODY></HTML>

Page 33: Professional Home Page  : PHP

Escaped characters

 \n BBBBBBB    \r carriage    \t horizontal tab  BB   backslash  BB   BBBBBB BBBB     \" BBBBBB-BBBBB\0nnn any octal byte\xnn any hexadecimal byte\\ backslash

Page 34: Professional Home Page  : PHP

Variableไม ติ�องประก�ศติ�วัแปรก อนก�รใชื่�ง�นไม ติ�องก��หนดชื่น�ดของติ�วัแปรก อนก�รใชื่�ง�นPHP จึะมองจึ�กก�รใส ให�ก�บติ�วัแปรน�"น ๆ เชื่ น

$X = ‘A12”; // ติ�วัแปร $X เป�นชื่น�ด string$Y = “123”; // ติ�วัแปร $X เป�นชื่น�ด string$Z = “ABC”; // ติ�วัแปร $X เป�นชื่น�ด string$I = 123; // ติ�วัแปร $I เป�นชื่น�ด integer$J = 1.23; // ติ�วัแปร $X เป�นชื่น�ด floating-point

( ห�กใชื่� “ ” หร อ ‘ ’ ก��หนดคื �ให�ก�บติ�วัแปร PHP จึะมองวั �ติ�วัแปรเป�น string ทำ�นทำ�)

Page 35: Professional Home Page  : PHP

Expressions คื อ ก�ร assign คื �จึ�กทำ�งขวั�ม อม�ทำ�งซั��ยม อ

ติ�วัอย �ง 5 1$b= ($a= ) + ; // ผู้ลทำ��ได� $a = 5 และ $b = 6 $b = $a = 5 + 1; // ผู้ลทำ��ได� $a และ $b =

6 ม� 2 ชื่น�ดคื อ pre-increment และ post-increment

pre-increment คื อ ++$x โดยทำ�� $x คื อติ�วัแปรใด ๆ ใน ก�รทำ��ง�นจึะทำ��ก�รเพั��มคื �ก อนทำ��จึะ assign คื �ให�ติ�วัแปร

เชื่ น $a = 5; $b = ++$a;

// ผู้ลทำ��ได� $a และ $b = 6

Page 36: Professional Home Page  : PHP

Expressions (ติ�อ)

post-increment คื อ $x++ โดยทำ�� $x คื อ ติ�วัแปรใด ๆ ก�รทำ��ง�นจึะ assign คื �ให�ติ�วัแปร

ก อนแล�วัจึ�งเพั��มคื � เชื่ น

$a = 5;$b = $a++;

// ผู้ลทำ��ได� คื อ $a = 6 และ $b = 5

Page 37: Professional Home Page  : PHP

Expressions (ติ�อ)$first ? $second : $third หม�ยถึ�ง ถึ�� $first เป�นจึร�ง ( ม�คื �ม�กกวั � 0) ให�

process $second ถึ�� $first เป�นเทำ4จึ ( ม�คื �เป�น 0) ให� process $third

ติ�วัอย �ง $a = 1; // กรณี�เป�นจึร�ง $b = 2; $c = 2; $a ? $b++ : $c++; // เม �อ 1$a = (เป�นจึร�ง ) เพั��มคื � $b เม �อผู้ �นก�ร process

จึะได�คื �เป�น$a = 1; $b = 3; $c = 2; (เหม อน

เด�ม)

Page 38: Professional Home Page  : PHP

Expressions (ติ�อ)

ติ�วัอย �ง $a = 0; // กรณี�เป�นเทำ4จึ $b = 2; $c = 2; $a ? $b++ : $c++; // เม �อ B B B0 เป�นเทำ4จึ ) เพั��มคื � $c เม �อ

ผู้ �นก�ร process จึะได�คื �เป�น$a = 0; $b = 2; Bเหม อนเด�ม)

$c = 3;

Page 39: Professional Home Page  : PHP

String Operation

PHP ม� operator ทำ��เก��ยวัข�องก�บ B 1

ติ�วัคื อ "."ม�วั�ธ์�ใชื่�ง�นด�งน�"

ติ�วัอย �งทำ�� $a = "Hi ! ";

BBB BB BB BPHP" ; $b จึะม�คื �เป�น Hi ! PHP

Page 40: Professional Home Page  : PHP

Operator Precedence

PHP จึะให�คืวั�มส��คื�ญก�บล��ด�บในก�รด��เน�นก�ร ของติ�วัด��เน�นก�รไม เทำ �ก�น

ติ�วัอย�าง 16*3 ผู้ลล�พัธ์:เป�น 19 ไม ใชื่

21

เพัร�ะ PHP จึะให�คืวั�มส��คื�ญก�บติ�วัด��เน�นก�ร B* ก�รคื'ณี ) ม�กกวั � B Bก�รบวัก) : น� �นคื อคื'ณี

ก อนแล�วัจึ�งบวัก

Page 41: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

IFโคืรงสร��ง

if (expr);

ก�รทำ��ง�น PHP จึะ execute statement เม �อ expr เป�นจึร�ง นอกจึ�กน�"นห�กติ�องก�รให�

ทำ��ม�กกวั � 1 statement ก4ส�ม�รถึทำ��ได�โดย ใส { } คืร อม BBBBBB BBBB

Page 42: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

IF (ติ�อ)ติ�วัอย �ง

if ($x != $y) print "$x is not equal to $y";

ถึ��คื �เร��มติ�นเป�น $x = 1 และ $y = 1 เม �อ execute คื��ส��งแล�วัจึะไม ม�ก�รแสดงผู้ลใด ๆ ทำ�"งน�"เพัร�ะเง �อนไข

ของ if เป�นเทำ4จึ แติ ถึ��ห�กวั �คื �เร��มติ�นเป�น $x = 10 และ $y = 2 จึะได�

ผู้ลล�พัธ์:จึ�กก�ร execute เป�น 10 is not equal to2

Page 43: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Elseโคืรงสร��ง

if (expr) 1statement ; BBBB 2 ;

Page 44: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

El se ( ติ อ)โคืรงสร��ง

if (expr) { statement1 ; statement2 ; }else{ statement3 ; statement4 ; }

Page 45: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Else (ติ�อ)ติ�วัอย �ง

if ($x > $y) { print "$x is greater than $y"; } else{ print "$x is not greater than

$y";}

จึะได�วั � statement ของ else จึะถึ'ก execute ก4ติ อ เม �อเง �อนไขของ if เป�นเทำ4จึ น��นคื อ $x ม�คื �น�อยกวั � หร อ

เทำ �ก�บ $y

Page 46: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Elseifโคืรงสร��ง

1if (expr ) { 1statement ; 2 ;

} elseif (expr2) { 3statement ; 4statement ;

} else { 5 ;}

Page 47: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Elseif ก�รทำ��ง�น

ติรวัจึสอบเง �อนไข 1 ก อน ถึ��เป�นจึร�งจึะทำ�� 12statement , ออก

ถึ�� 1expr เป�นเทำ4จึ จึ�งจึะติรวัจึสอบ 2expr ถึ��เป�น จึร�งก4จึะทำ�� 3 ,4statement ถึ��เป�นเทำ4จึก4จึะหล+ดไป

ทำ�� 5statement ของ BBBB

Page 48: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

If () : … endif; ก�รใชื่� code PHP คืวับคื+มก�รแสดง code HTML โดยใชื่�

IFโคืรงสร��ง

<?php if (expr): ?> HTML CODE <?php endif; ?>

ก�รทำ��ง�น- ติรวัจึสอบ expr ถึ��เป�นจึร�งจึะแสดง HTML CODE- เป�นเทำ4จึ ไม แสดงอะไร

Page 49: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

If () : … endif; ก�รใชื่� code PHP คืวับคื+มก�รแสดง code

B BB B โดยใชื่� else และelseifโคืรงสร��ง

1<?php if (expr ): ?> 1HTML CODE

2<?php elseif (expr ): ?> 2HTML CODE

<?php else: ?> 3HTML CODE

<?php endif; ?>

Page 50: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Whileโคืรงสร��ง

while (expr) statement

ก�รทำ��ง�น while จึะcheck เง �อนไข expr ก อนทำ��จึะ process

statement ทำ��อย' ใน block ของ while

Page 51: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

While (ติ�อ)ก�รทำ��ง�น

- ถึ�� expr เป�นจึร�งก4จึะทำ��ง�นติ�มBBBBBB BBB ภ�ยใน BBBBB โดยทำ��วันซั�"� ๆ ไป

เร �อย ๆ จึนกวั � expr จึะม�คื �เป�นเทำ4จึ- คืวัรระวั�งในก�รทำ��เง �อนไข เพัร�ะวั �อ�จึทำ��ให�

เก�ดก�รวันรอบไม ร' �จึบ (infinite loop) ได� ถึ��ม� ก�ร check ทำ��ไม ด�

Page 52: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

ติ�วัอย �ง$x=1;while ($x <= 10) {print "$x ";$x++;}

ผู้ลล�พัธ์: คื อ 0 1 2 3 4 5 6 7 8 9 10

Page 53: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Do … while ก�รทำ��ง�น จึะทำ��ก อน ติรวัจึสอบทำ�หล�ง โคืรงสร��ง

do{ 1statement ; 2 ; 3statement ;

} while (expr);

Page 54: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

Do … while (ติ�อ)ติ�วัอย �ง

0$x= ; do {

print "$x "; } while ($x > 0);

น�"จึะได�ผู้ลล�พัธ์:เป�น 0 ถึ��เปล��ยนเป�น while จึะ ไม ม�ก�รแสดงผู้ลใด ๆ

Page 55: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

For ม�โคืรงสร��งก�รใชื่�ง�น เหม อน ก�นก�บ f or ในภ�ษ�

C

for (expr1;expr2;expr3) statement;

ก�รทำ��ง�น- expr1 จึะถึ'กทำ��ก อน โดยม�เทำ�ยบก�บ expr 2

ถึ��เป�นจึร�งจึะทำ��ติ อทำ�� expr3 ถึ��เป�นเทำ4จึจึะหย+ดทำ��

Page 56: Professional Home Page  : PHP

Control Structures Control Structures คื��ส��งทำ��ใชื่�ในคื��ส��งทำ��ใชื่�ใน ก�รคืวับคื+มของ ก�รคืวับคื+มของ PHPPHP

For (ติ�อ)ติ�วัอย �ง

1 10for ($x= ;$x< ;$x++) { print "$x "; }

ผู้ลทำ��ได� 123456789

Page 57: Professional Home Page  : PHP

ก�รสร��ง ก�รสร��ง Gift Gift ด�วัย ด�วัย PHPPHP

ติ�องเร�ยกใชื่� function GD Library ของFunction Imag ของ PHP

ก�รเร�ยกใชื่�ง�นจึร�ง <img src=“ ชื่ �อ แฟ@มข�อม'ลภ�พัทำ��ทำ��

เสร4จึแล�วั ?text = ข�อคืวั�มทำ��ติ�องก�ร”>ติ�วัอย �ง

<img src=“dbtext.php?text= Hello + PHP”>

Page 58: Professional Home Page  : PHP

การีที่$าให% PHP สามารีถอ�านไที่ยได%

หล�กก�ร- ก�รติ�ดเอ�คื��ออกม�ทำ��ละติ�วั แล�วัแปลงเป�น ASCII

แล�วัแปลงกล�บไปเป�น character โดยก�รน��เอ� source น�"ไปแทำรกไวั�ส วันบน

for ($I = 0; $I < strlen($thai); $I++) { $dtext=$dtext. #&#.ord(substr($thai,$I,1)).”;”; }