57
1 pter 4 Server-side Technologies pter 4 Server-side Technologies Details 4.1 PHP 4.2 ASP 4.3 JSP 4.4 Perl and CGI

Chapter 4 Server-side Technologies

  • Upload
    edward

  • View
    65

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 4 Server-side Technologies. Details 4.1 PHP 4 .2 ASP 4.3 JSP 4.4 Perl and CGI. 4.1 PHP. PHP :Hypertext Preprocessor - abbreviated from Personal Home Pages - one of the most popular server-side scripting languages - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 4   Server-side Technologies

1

Chapter 4 Server-side TechnologiesChapter 4 Server-side Technologies

Details

41. PHP4 .2 ASP4.3 JSP4.4 Perl and CGI

Page 2: Chapter 4   Server-side Technologies

2

4.1 PHP

PHP :Hypertext Preprocessor

- abbreviated from Personal Home Pages - one of the most popular server-side scripting languages - was created in 1994 by Rasmas Lerdorf - an open-source technology - platform independent - supports a large number of databases

more : http://www.php.net

Page 3: Chapter 4   Server-side Technologies

การเตร�ยมการเพื่� อเขี�ยนโปรแกรมภาษา PHPการเตร�ยมการเพื่� อเขี�ยนโปรแกรมภาษา PHP

เคร� องคอมพื่�วเตอร�อย�างน�อย 1 เคร� อง โดยปกต�การพื่�ฒนาระบบงานบนเว"บจะต�องม�เคร� องคอมพื่�วเตอร�ที่� ที่%าเป&น Web Server และ Web browser แต�ถ้�าม�งบประมาณจ%าก�ดในการจ�ดหาเคร� องคอมพื่�วเตอร�ก"สามารถ้ใช้�เคร� องคอมพื่�วเตอร�เพื่�ยงเคร� องเด�ยวก"ได� โดยให�เคร� องคอมพื่�วเตอร�น�/ที่%าหน�าที่� เป&นที่�/ง Web Server และ Web browser

ระบบปฏิ�บ�ต�การ Windows หร�อ Unix หร�อ Linux โปรแกรม Web Server ซึ่2 งสามารถ้เล�อกใช้�โปรแกรม

อะไรก"ได� เช้�น Apache, Internet Information Server (IIS), OmniHTTPd เป&นต�น

Page 4: Chapter 4   Server-side Technologies

PHP Engine หร�อต�วแปลภาษา PHP ซึ่2 งเป&นโปรแกรมที่� จะที่%าหน�าที่� แปลภาษา PHP เป&นภาษาเคร� องเพื่� อให�คอมพื่�วเตอร�สามารถ้ที่%างานและประมวลผลได�ตามที่� ต�องการ

โปรแกรมจ�ดการฐานขี�อม5ล ซึ่2 งโปรแกรมภาษา PHP สามารถ้เล�อกใช้�ฐานขี�อม5ลได�หลายประเภที่ เช้�น dBase, IMAP, MySql, MS Access, Oracle เป&นต�น

หากเล�อกใช้�ฐานขี�อม5ล MySql ควรใช้� PhpMyadmin ซึ่2 งเป&นโปรแกรมที่� ใช้�จ�ดการฐานขี�อม5ล MySql โดยเฉพื่าะ

โปรแกรม Web Authoring และ Editor ค�อโปรแกรมที่� ใช้�ในการสร�างเว"บเพื่จ เช้�น Macromedia Dreamweaver, Microsoft Frontpage, Editplus, Notepad เป&นต�น

การเตร�ยมการเพื่� อเขี�ยนโปรแกรมภาษา PHPการเตร�ยมการเพื่� อเขี�ยนโปรแกรมภาษา PHP

Page 5: Chapter 4   Server-side Technologies

5

Characteristic of PHP

- file extension is .php- PHP statements are terminated with a semicolon ;- one-line comment begins with // or #- multiline comment begins with /* and end with */

Page 6: Chapter 4   Server-side Technologies

6

ร5ปแบบการฝั8ง PHP script ในเอกสาร

แบบที่� 1 แที่รก script ด�วยเคร� องหมาย <?php และ ?>

<?php echo “This is also simple, do like this <br>”; ?>

แบบที่� 2 ก%าหนดด�วย tag <script></script>

<script language = “php”> echo “Hello php<br>”; </script>

แบบที่� 3 ครอบส�วนที่� เป&น script ด�วย <? และ ?> เร�ยกว�า short style <? echo “This is the simplest way<br>”; ?>

Page 7: Chapter 4   Server-side Technologies

7

ร5ปแบบการฝั8ง PHP script ในเอกสาร (ต�อ)

แบบที่� 4 ใช้� tag ขีอง ASP

<% echo “you may use ASP-style tags”; %>

หมายเหต9 จะต�องก%าหนด asp_tags ในเอกสาร config ก�อน จ2งจะใช้�ได� (ใช้�ได�ก�บ php version 304 เที่�าน�/น)

Page 8: Chapter 4   Server-side Technologies

8

Basic PHP<!DOCTYPE html PUBLIC -" //W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">< -- ! first.php --><?php $name = "LunaTic"; // declaration?><html xmlns=http://www.w3.org/1999/xhtml"> <head> <title>A simple PHP document</title> </head> <body style = "font-size : 2em"> <p> <strong> Welcome to PHP, <?php print ( "$name " ); ?>! </strong> </p> </body></html>

Page 9: Chapter 4   Server-side Technologies

9

Define PHP Variable names

-use characters, number, underscore and $ $var_name

- first character must not be number

- don’t use name as same as built-in functions or any keyword

Page 10: Chapter 4   Server-side Technologies

10

PHP Keywords

and false or break for requirecase foreach returnclass function staticcontinue global switchdefault if thisdo include trueelse list varelseif new virtualextends not xor

while

Page 11: Chapter 4   Server-side Technologies

11

PHP data type

Data type Description

int, integer

float, doublestring

bool, BooleanarrayobjectNULL

Whole numbers without decimal pointReal numbersText enclosed in either single or double quotesTrue of falseGroup of the same type elements Group of associated data and methodsNo value

Page 12: Chapter 4   Server-side Technologies

12

Integer

เขี�ยนอย5�ในร5ปขีองเลขีฐานได� ค�อฐาน 10 ฐาน 8 : ใช้� 0 น%าหน�า และ ฐาน 16 : ใช้� 0x น%าหน�า

$i = 254; $j = -431; $k = 0123; # ม�ค�าเที่�าก�บ (123 ฐาน 8 ม�ค�าเที่�าก�บ 83)$m = 0x12; # ม�ค�าเที่�าก�บ (12 ฐาน 16 ม�ค�าเที่�าก�บ 18 )

Page 13: Chapter 4   Server-side Technologies

13

Float

เขี�ยนในร5ปแบบขีองเลขีที่ศน�ยมและเลขียกก%าล�ง

$i = 6.254; $j = -2e12; # ม�ค�าเที่�าก�บ -2 x 1012

String

ใช้�จ�ดเก"บต�วอ�กษรและขี�อความที่� วไป โดยจะต�องอย5�ในเคร� องหมาย double quotes (“ ”)

Page 14: Chapter 4   Server-side Technologies

14

String

รห�สควบค9มพื่�เศษที่� ใช้�ก�บ string

ส�ญล�กษณ� ความหมาย\n ใช้�ขี2/นบรรที่�ดใหม�\f เล� อนเคอร�เซึ่อร�ไปต�นบรรที่�ด\t ใช้�เล� อน Tab\\ ใช้�พื่�มพื่�เคร� องหมาย \ (Backslash)\$ ใช้�พื่�มพื่�เคร� องหมาย $ (Dollar)\” ใช้�พื่�มพื่�เคร� องหมาย “ (Double Quote)\0 ก%าหนดจ9ดส�/นส9ดขีอง string

Page 15: Chapter 4   Server-side Technologies

15

Define data type

- we can define data type by assign value to variables <? $num = 60-30; echo $num.“<br>”; $num = “test”; echo $num; ?>- type casting <? $x = 23.4; $y = 12.7; $z = $x+$y; echo “z = ”.$z.“<br>”; $x = (integer)$x; $y = (integer)$y; $z = $x+$y; echo “z = ”.$z; ?>

ผล30test

ผลz = 36.1z = 35

Page 16: Chapter 4   Server-side Technologies

16

Define data type

- variable variables <? $num = “y”; $$num = 6; echo $y. “<br>”; echo $num; ?>

- constants int define (string name, mixed value)

<? define (“value”, “constant data”); define (“x”, 12); define (“y”,3); $z = $x+$y; echo “value = ”.$value. “<br>”; echo “z = ”.$z; ?>

ผล6y

ผลvalue = constant dataz = 15

Page 17: Chapter 4   Server-side Technologies

17

Variable function

gettype - use for show type of variable string gettype(mixed var)

settype - use for define data type of variable int settype(string var, string type)

Page 18: Chapter 4   Server-side Technologies

18

Example

<? $value = 26.425; echo “value = ”; echo gettype($value). “<br>”; settype($value, “integer”); echo “type of variable<br>”; echo “value = ”; echo gettype($value);?>

ผลvalue = doubletype of variablevalue = integer

Page 19: Chapter 4   Server-side Technologies

19

Operator

Arithmetic operator

ส�ญล�กษณ� ช้� อ ต�วอย�าง

+ Addition $x+$y

- Subtraction $x-$y

* Multiplication $x*$y

/ Division $x/$y

% Modulus $x%$y

Page 20: Chapter 4   Server-side Technologies

20

Operator

String operator การน%าขี�อม5ลมารวมก�นเป&นขี�อความเด�ยวก�นใช้�เคร� องหมายจ9ด (.)

Assignment operator เป&นการก%าหนดค�าให�ก�บต�วแปร ใช้�เคร� องหมายเที่�าก�บ (=)

- Combination Assignment operators ได�แก� +=, -=, *=, /=, %=, .= เช้�น $x+= 2 - Pre/Post Increment and Decrement ได�แก� $x++, ++$x, $y--, --$y

Page 21: Chapter 4   Server-side Technologies

21

Operator

Comparison operator การน%าเปร�ยบเที่�ยบค�า 2 ค�า เพื่� อค�นค�าเป&น true หร�อ false

sym name example

== equals $y == $x

!= not equal $y != $x

<> not equal $y <> $x

< less than $y < $x

> greater than $y > $x

<= less than or equal to $y <= $x

>= greater than or equal to

$y => $x

Page 22: Chapter 4   Server-side Technologies

22

Operator

Logical operator ค%าส� งเง� อนไขีที่างตรรกะ

sym name example

! NOT !$x

&& AND $y && $x

|| OR $y || $x

Page 23: Chapter 4   Server-side Technologies

23

Control structure

- if statement

ร5ปแบบ if (condition) { statement; …… }

ต�วอย�าง if ($height > 0 && $width > 0) { $area = $height*$width; echo “Area of square = ” .$area; }

Page 24: Chapter 4   Server-side Technologies

24

Control structure

- if…else statementร5ปแบบ if (condition) { statement; ……} else { statement; ……}

ต�วอย�าง if ($height > 0 && $width > 0) { $area = $height*$width; echo “Area of square = ” .$area; } else { echo “Please enter new value”;}

Page 25: Chapter 4   Server-side Technologies

25

Control structure

- if…elseif statement (nested if statement)ร5ปแบบ if (condition1) { statement; ……} elseif (condition2) { statement; ……}

ต�วอย�าง if ($score < 0) echo “Enter new score\n”; elseif ($score >= 80.0) $grade = “G”; elseif ($score >= 60.0) $grade = “P”;

Page 26: Chapter 4   Server-side Technologies

26

Control structure

- switch statement

ร5ปแบบ switch (variable) { case value1 : statement; break; case value2 : statement; break; default : statement; }

Page 27: Chapter 4   Server-side Technologies

27

Loop structure

- for statement ร5ปแบบ for (initial value; condition; change value) { statement; …… }

- while statement ร5ปแบบ while (condition) { statement; …… }

- do…while statement ร5ปแบบ do { statement; …… } while (condition);

Page 28: Chapter 4   Server-side Technologies

28

Array ที่� ม� index เป&นต�วเลขี

การสร�าง การสร�าง arrayarray 1) ใช้�ฟั8งก�ช้�น array() array array([mixed….]);

$province = array(“Bangkok”, “Trad”, “Chiang Mai”);for ($i=0;$i<3;$i++){ echo “province[”.$i. “] = ”; echo $province[$i]; echo “<br>”;}

Page 29: Chapter 4   Server-side Technologies

29

2) ใช้�ฟั8งก�ช้�น range() array range(int ค�าเร� มต�น, int ค�าส5งส9ด);

$arr = range(4,9);for ($i=0;$i<count($arr);$i++){ echo "arr[".$i."] = "; echo $arr[$i]; echo "<br>";}

Page 30: Chapter 4   Server-side Technologies

30

Array ที่� ม� index เป&นต�วอ�กษร

การเขี�าถ้2งขี�อม5ล การเขี�าถ้2งขี�อม5ล array array - ฟั8งก�ช้�น each() เป&นฟั8งก�ช้�นที่� ค�นค�า 2 ค�าค�อ index และค�าขีองขี�อม5ลโดยที่� ค�า index จะค�นด�วยฟั=ลด� key และ ค�าขีองขี�อม5ลจะค�นด�วยฟั=ลด� value

- ฟั8งก�ช้�น list ที่%างานเช้�นเด�ยวก�บ each() แต�จะแยก array ออกจากก�นและเก"บในต�วแปรที่� ก%าหนด

- ฟั8งก�ช้�น reset ใช้�ส%าหร�บย�ายพื่อยน�เตอร� ไปช้�/ที่� ค�าแรกขีอง array

Page 31: Chapter 4   Server-side Technologies

31

Array ที่� ม� index เป&นต�วอ�กษร

การสร�าง การสร�าง array array ใช้�ฟั8งก�ช้�น array() $arr_list=array("Bangkok"=>36,"Tak"=>10,"Trad"=>15);while ($element = each($arr_list)){ echo "province ["; echo $element["key"]; echo "] = "; echo $element["value"]; echo "<br>";}

Page 32: Chapter 4   Server-side Technologies

32

Example

$arr_list=array("Bangkok"=>36,"Tak"=>10,"Trad"=>15);while (list($province,$amphur) = each($arr_list)){ echo "province [".$province."] have "; echo $amphur." amphurs"; echo "<br>";}

Page 33: Chapter 4   Server-side Technologies

33

Array หลายม�ต�ที่� ม� index เป&นต�วเลขี

$arr_list = array(array("BKK","Bangkok",36), array("TK","Tak",10),

array("TR","Trad",15));for($row=0;$row<3;$row++){ for($col=0;$col<3;$col++) echo $arr_list[$row][$col]." "; echo "<br>";}

Page 34: Chapter 4   Server-side Technologies

34

Array หลายม�ต�ที่� ม� index เป&นต�วอ�กษร$arr_list=array(array("Code"=>"BKK", "Des"=>"Bangkok","amphur"=>36), array("Code"=>"TK","Des"=>"Tak","amphur"=>10), array("Code"=>"TR","Des"=>"Trad","amphur"=>15));

for($row=0;$row<3;$row++){ echo $arr_list[$row]["Code"]." "; echo $arr_list[$row]["Des"]." "; echo $arr_list[$row]["amphur"]."<br>";}

Page 35: Chapter 4   Server-side Technologies

35

การใช้�งาน string

การต�ดช้�องว�างในสตร�ง- trim() ส%าหร�บต�ดช้�องว�างที่�/งหมดที่� อย5�ด�านหน�าและด�านหล�งขีองสตร�งที่�/งไป

- ltrim() ต�ดช้�องว�างที่� อย5�ด�านหน�า(ที่างซึ่�าย ) ที่�/งไป

- chop() ต�ดช้�องว�างที่� อย5�ด�านหล�ง (ที่างขีวา ) ที่�/งไป

Page 36: Chapter 4   Server-side Technologies

36

<?$str1 =" Computer Science\n\n";print "String1 = ".$str1."<br>";echo "Length of String1 =".strlen($str1)."<br>";$str2 = trim($str1);echo "Length of String2 =".strlen($str2)."<br>";?>

trim()

Page 37: Chapter 4   Server-side Technologies

37

<?$str1 =" Computer Science\n\n";print "String1 = ".$str1."<br>";echo "Length of String1 =".strlen($str1)."<br>";$str2 = ltrim($str1);echo "Length of String2 =".strlen($str2)."<br>";?>

ltrim()

Page 38: Chapter 4   Server-side Technologies

38

<?$str1 =" Computer Science\n\n ";print "String1 = ".$str1."<br>";echo "Length of String1 =".strlen($str1)."<br>";$str2 = chop($str1);echo "Length of String2 =".strlen($str2)."<br>";?>

chop()

Page 39: Chapter 4   Server-side Technologies

39

<?$str =" Computer\n Science\nChiang Mai\n\n";print "String = ".$str."<br>";echo “use nl2br() <br>";echo "String = ".nl2br($str);?>

nl2br() เปล� ยน \n ให�เป&น <br>

Page 40: Chapter 4   Server-side Technologies

40

การแสดงต�วเลขีโดยใช้�ค%าส� ง echo น�/น จะไม�สามารถ้จ�ดร5ปแบบได� แต�ถ้�าใช้�ฟั8งก�ช้�น printf() และ sprintf() จะสามารถ้จ�ดร5ปแบบได�

การแสดงขี�อม5ลที่างจอภาพื่

<?$str =" Computer\n Science\nChiang Mai\n\n";printf("String = %s<br>",$str);print “<em>use nl2br () </em><br>";echo "String = ".nl2br($str);?>

Page 41: Chapter 4   Server-side Technologies

41

ค�า args ส%าหร�บการแสดงร5ปแบบ

ช้น�ด ความหมายbcdfosxX

แสดงเป&นเลขีฐานสองแสดงเป&นต�วอ�กขีระตามรห�สแอสก�คร�/งละ 1 ต�วแสดงเป&นเลขีฐานส�บแสดงเป&นเลขีจ%านวนจร�งแสดงเป&นเลขีฐานแปดแสดงเป&นต�วอ�กษร (String)แสดงเป&นเลขีฐานส�บหกและเป&นต�วอ�กษรต�วเล"ก a-fแสดงเป&นเลขีฐานส�บหกและเป&นต�วอ�กษรต�วใหญ� A-F

Page 42: Chapter 4   Server-side Technologies

42

ต�วอย�างการแสดงผล$val = 109.2536859;printf("binary = %b<br>",$val);printf("decimal = %d<br>",$val);printf("character = %c<br>",$val);printf("floating = %.4f<br>",$val);printf("octal = %o<br>",$val);printf("string = %0.5s<br>",$val);printf("hexalower = %x<br>",$val);printf("hexaupper = %X<br>",$val);$val1 = sprintf("%b",$val);$val2 = sprintf("%d",$val);$val3 = sprintf("%c",$val);echo "binary = ".$val1."<br>";echo "decimal = ".$val2."<br>";echo "character = ".$val3."<br>";

Page 43: Chapter 4   Server-side Technologies

43

การเปล� ยนร5ปแบบขีองต�วอ�กษรฟั8งก�ช้�น ค%าอธิ�บายstrtoupper()strtolower()ucfirst()

ucwords()

แปลงประโยคให�เป&นต�วพื่�มพื่�ใหญ�แปลงประโยคให�เป&นต�วพื่�มพื่�เล"กแปลงต�วอ�กษรต�วแรกขีองประโยคให�เป&นต�วพื่�มพื่�ใหญ�แปลงต�วอ�กษรต�วแรกขีองค%าให�เป&นต�วพื่�มพื่�ใหญ�

ต�วอย�างค%าส� ง $str = “Please ENTER your name”;strtoupper($str);strtolower($str);ucfirst($str);ucwords($str);

Page 44: Chapter 4   Server-side Technologies

44

การใช้� Regular Expression

regular expression ใช้�ส%าหร�บการเปร�ยบเที่�ยบและแที่นที่� ขี�อม5ลแบบสตร�ง ซึ่2 งคล�ายก�บการใช้�ฟั8งก�ช้�นเปร�ยบเที่�ยบและแที่นที่� ขีองสตร�ง แต� regular expressionจะสามารถ้ก%าหนดร5ปแบบขีองสตร�งได�มากกว�า เพื่� อใช้�ในการค�นหาสตร�งที่� ใกล�เค�ยงก�นได�

Page 45: Chapter 4   Server-side Technologies

45

ฟั8งก�ช้�นส%าหร�บตรวจสอบค�าสตร�ง

ฟั8งก�ช้�นการค�นหาสตร�งย�อย - ereg() ใช้�ส%าหร�บตรวจสอบขี�อความและต�วอ�กษร ฟั8งก�ช้�นจะต�ความต�วอ�กษรพื่�มพื่�ใหญ�และต�วอ�กษรพื่�มพื่�เล"กแตกต�างก�น

ร5ปแบบ int ereg(string pattern, string string) - eregi() ใช้�ส%าหร�บการตรวจสอบขี�อความและต�วอ�กษร โดยจะต�ความต�วอ�กษรพื่�มพื่�ใหญ�และต�วพื่�มพื่�เล"กเหม�อนก�น

ร5ปแบบ int eregi(string pattern, string string)

Page 46: Chapter 4   Server-side Technologies

46

ต�วอย�าง

<? $str1 = "Computer Science, Chiang mai";$str2 = "computer science, Chiang Mai";$com = "Chiang mai";echo "Comparision string1<br>";if (ereg($com,$str1)) echo "Found String<br>";else echo "Not found <br>";echo "Comparision string2<br>";if (ereg($com,$str2)) echo "Found String<br>";else echo "Not found <br>";echo "Comparision string2 with eregi<br>";if (eregi($com,$str2)) echo "Found String<br>";else echo "Not found <br>";?>

Page 47: Chapter 4   Server-side Technologies

47

ฟั8งก�ช้�นการแที่นที่� สตร�งย�อย

- ereg_replace() ใช้�ส%าหร�บแที่นที่� ขี�อความและต�วอ�กษร ฟั8งก�ช้�นจะต�ความต�วอ�กษรพื่�มพื่�ใหญ�และต�วอ�กษรพื่�มพื่�เล"กแตกต�างก�น

ร5ปแบบ string ereg_replace(string pattern, string replace,string string) - eregi_replace() ใช้�ส%าหร�บแที่นที่� ขี�อความและต�วอ�กษร โดยจะต�ความต�วอ�กษรพื่�มพื่�ใหญ�และต�วพื่�มพื่�เล"กเหม�อนก�น

ร5ปแบบ string eregi_replace(string pattern, string replace,string string)

Page 48: Chapter 4   Server-side Technologies

48

ต�วอย�าง

<? $str1 = "Computer Science, Chiang mai";echo "before replace string = $str1<br>";echo "after replace string = ";echo ereg_replace("Chiang mai","CMU",$str1)."<br>";echo "before replace string = $str1<br>";echo "after replace string = ";echo eregi_replace("computer scienCE","Cs",$str1);?>

Page 49: Chapter 4   Server-side Technologies

49

ฟั8งก�ช้�นการแบ�งสตร�ง

- split() ใช้�ส%าหร�บแบ�งสตร�งแล�วไปเก"บไว�ในอาร�เรย�ร5ปแบบ array split(string pattern, string search, int [max]) - spliti() ใช้�ส%าหร�บแบ�งสตร�ง โดยจะต�ความต�วอ�กษรพื่�มพื่�ใหญ�และต�วพื่�มพื่�เล"กเหม�อนก�น

ร5ปแบบ array spliti(string pattern, string search, int [max])

Page 50: Chapter 4   Server-side Technologies

50

ต�วอย�าง<? $str1 = "Computer Science, Chiang mai";echo "after substring<br>";$array1 = split(" ",$str1);for ($i=0;$i<count($array1);$i++) echo "array1[$i]=$array1[$i]<br>";

echo "after substring with c<br>"; $array3 = spliti("c",$str1);for ($i=0;$i<count($array3);$i++) echo "array3[$i]=$array3[$i]<br>";?>

Page 51: Chapter 4   Server-side Technologies

• ฟั8งก�ช้�นที่� ไม�ม�การส�งค�ากล�บ

ร5ปแบบ

function function_name (argument_list ) { statement; ..... }

• ฟั8งก�ช้�นที่� ม�การส�งค�ากล�บ

ร5ปแบบ

function function_name (argument_list ) { statement; ..... return ค�าที่� จะส�งกล�บ; }

User-Defined Function

Page 52: Chapter 4   Server-side Technologies

52

User-Defined Function

ตั�วอย่�าง<?php function check_length($data) { if (strlen($data) < 6) return "The data was too small"; else return "That data was fine"; } ?>การเร�ย่กใช้� สามารถเร�ย่กใช้�ที่��จากที่��ใดก�ได�ในไฟล์� php<?php $example = "qwertyuiop";echo check_length($example); ?>

Page 53: Chapter 4   Server-side Technologies

ตั�วอย่�าง Pass by value

function one ( $parameter ) { $parameter++;}$a = 10 ;one( $a ) ;echo “ a = $a <BR>” ;

ผล์ล์�พธ์�ของโปรแกรมคื&อa = 10

Page 54: Chapter 4   Server-side Technologies

ตั�วอย่�าง Pass by reference function one ( &$parameter ) { $parameter++;}$a = 10 ;one( $a ) ;echo “ a = $a <BR>” ;ผล์ล์�พธ์�ของโปรแกรมคื&อa = 11

Page 55: Chapter 4   Server-side Technologies

55

ต�วอย�าง PHP with Form

<html> <body> <form action="formprocess.php" method="POST" name="fmProcess"> ช้&�อ: <input name="firstname" type="text" size="32" maxlength="30"> <br> นามสก'ล์: <input name="lastname" type="text" size="32" maxlength="30"> <br> <input name="btnSubmit" type="submit" value="บั�นที่)ก"> <input name="btnReset" type="reset" value="ย่กเล์*ก"> </form> </body> </html>

form.html

Page 56: Chapter 4   Server-side Technologies

56

ต�วอย�าง PHP with Form

<html> <body> <?php echo "ช้&�อ: " . $_POST['firstname'] . "<br>"; echo "นามสก'ล์: " . $_POST['lastname'] . "<br>"; ?> </body> </html>

formprocess.php

Page 57: Chapter 4   Server-side Technologies

57

การที่%างานขีอง Method : Post , Get

- POST สงขอม+ล์โดย่สงขอม+ล์สงไปก�บั HTTP Request

- GET สงขอม+ล์โดย่การแปะคืาเปน URL Parameter (Query String) ไปก�บั URL ( ขอม+ล์ที่��สงจะแสดงอย่+บัน URL ของเว�บัเบัราเซอร)

NOTE: ว*ธ์�การสงขอม+ล์แบับั GET ไมคืวรใช้ก�บัฟอรมที่��ม�การสงขอม+ล์ จ.านวนมาก รวมที่�/งไมคืวรใช้ในการสงขอม+ล์ที่��เปนคืวามล์�บั

เช้น username, password หร&อเล์ขที่��บั�ตัรเคืรด*ตั เปนตัน เน&�องจากว*ธ์�การสงแบับั GET น�/ ขอม+ล์ที่��เราสงจะถ+กแสดงบัน URL

เช้น http://localhost/phpweb/detail.php?empid=01020489