1 / 57

Chapter 4 Server-side Technologies

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

edward
Download Presentation

Chapter 4 Server-side Technologies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  3. การเตรียมการเพื่อเขียนโปรแกรมภาษา PHP • เครื่องคอมพิวเตอร์อย่างน้อย 1 เครื่อง โดยปกติการพัฒนาระบบงานบนเว็บจะต้องมีเครื่องคอมพิวเตอร์ที่ทำเป็น Web Server และ Web browser แต่ถ้ามีงบประมาณจำกัดในการจัดหาเครื่องคอมพิวเตอร์ก็สามารถใช้เครื่องคอมพิวเตอร์เพียงเครื่องเดียวก็ได้ โดยให้เครื่องคอมพิวเตอร์นี้ทำหน้าที่เป็นทั้ง Web Server และ Web browser • ระบบปฏิบัติการ Windows หรือ Unix หรือ Linux • โปรแกรม Web Server ซึ่งสามารถเลือกใช้โปรแกรมอะไรก็ได้ เช่น Apache, Internet Information Server (IIS), OmniHTTPd เป็นต้น

  4. การเตรียมการเพื่อเขียนโปรแกรมภาษา PHP • PHP Engine หรือตัวแปลภาษา PHP ซึ่งเป็นโปรแกรมที่จะทำหน้าที่แปลภาษา PHP เป็นภาษาเครื่องเพื่อให้คอมพิวเตอร์สามารถทำงานและประมวลผลได้ตามที่ต้องการ • โปรแกรมจัดการฐานข้อมูล ซึ่งโปรแกรมภาษา PHP สามารถเลือกใช้ฐานข้อมูลได้หลายประเภท เช่น dBase, IMAP, MySql, MS Access, Oracle เป็นต้น • หากเลือกใช้ฐานข้อมูล MySql ควรใช้ PhpMyadmin ซึ่งเป็นโปรแกรมที่ใช้จัดการฐานข้อมูล MySql โดยเฉพาะ • โปรแกรม Web Authoring และ Editor คือโปรแกรมที่ใช้ในการสร้างเว็บเพจ เช่น Macromedia Dreamweaver, Microsoft Frontpage, Editplus, Notepad เป็นต้น

  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 */

  6. รูปแบบการฝัง 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>”; ?>

  7. รูปแบบการฝัง PHP script ในเอกสาร (ต่อ) แบบที่ 4 ใช้ tag ของ ASP <% echo “you may use ASP-style tags”;%> หมายเหตุ จะต้องกำหนด asp_tags ในเอกสาร config ก่อน จึงจะใช้ได้ (ใช้ได้กับ php version 3.0.4 เท่านั้น)

  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>

  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

  10. PHP Keywords and false or break for require case foreach return class function static continue global switch default if this do include true else list var elseif new virtual extends not xor while

  11. PHP data type

  12. Integer เขียนอยู่ในรูปของเลขฐานได้ คือฐาน 10 ฐาน 8 : ใช้ 0 นำหน้า และ ฐาน 16 : ใช้ 0x นำหน้า $i = 254; $j = -431; $k = 0123; # มีค่าเท่ากับ (123 ฐาน 8 มีค่าเท่ากับ 83) $m = 0x12; # มีค่าเท่ากับ (12 ฐาน 16 มีค่าเท่ากับ 18 )

  13. Float เขียนในรูปแบบของเลขทศนิยมและเลขยกกำลัง $i = 6.254; $j = -2e12; # มีค่าเท่ากับ -2 x 1012 String ใช้จัดเก็บตัวอักษรและข้อความทั่วไป โดยจะต้องอยู่ ในเครื่องหมาย double quotes (“ ”)

  14. String รหัสควบคุมพิเศษที่ใช้กับ string

  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; ?> ผล 30 test ผล z = 36.1 z = 35

  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; ?> ผล 6 y ผล value = constant data z = 15

  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)

  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 = double type of variable value = integer

  19. Operator Arithmetic operator

  20. Operator String operator การนำข้อมูลมารวมกันเป็นข้อความเดียวกันใช้เครื่องหมายจุด (.) Assignment operator เป็นการกำหนดค่าให้กับตัวแปร ใช้เครื่องหมายเท่ากับ (=) - Combination Assignment operators ได้แก่ +=, -=, *=, /=, %=, .= เช่น $x+= 2 - Pre/Post Increment and Decrement ได้แก่ $x++, ++$x, $y--, --$y

  21. Operator Comparison operator การนำเปรียบเทียบค่า 2 ค่า เพื่อคืนค่าเป็น true หรือ false

  22. Operator Logical operator คำสั่งเงื่อนไขทางตรรกะ

  23. Control structure - if statement รูปแบบ if (condition) { statement; …… } ตัวอย่าง if ($height > 0 && $width > 0) { $area = $height*$width; echo “Area of square = ” .$area; }

  24. Control structure - if…else statement รูปแบบ if (condition) { statement; ……} else { statement; ……} ตัวอย่าง if ($height > 0 && $width > 0) { $area = $height*$width; echo “Area of square = ” .$area; } else { echo “Please enter new value”;}

  25. Control structure - if…elseif statement (nested if statement) รูปแบบ 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”;

  26. Control structure - switch statement รูปแบบ switch (variable) { case value1 : statement; break; case value2 : statement; break; default : statement; }

  27. Loop structure - for statement รูปแบบ for (initial value; condition; change value) { statement; …… } - while statement รูปแบบ while (condition) { statement; …… } - do…while statement รูปแบบ do { statement; …… } while (condition);

  28. Array ที่มี index เป็นตัวเลข การสร้าง array 1) ใช้ฟังก์ชัน array() array array([mixed….]); $province = array(“Bangkok”, “Trad”, “Chiang Mai”); for ($i=0;$i<3;$i++) { echo “province[”.$i. “] = ”; echo $province[$i]; echo “<br>”; }

  29. 2) ใช้ฟังก์ชัน range() array range(int ค่าเริ่มต้น, int ค่าสูงสุด); $arr = range(4,9); for ($i=0;$i<count($arr);$i++) { echo "arr[".$i."] = "; echo $arr[$i]; echo "<br>"; }

  30. Array ที่มี index เป็นตัวอักษร การเข้าถึงข้อมูล array - ฟังก์ชัน each() เป็นฟังก์ชันที่คืนค่า 2 ค่าคือ index และค่าของข้อมูล โดยที่ค่า index จะคืนด้วยฟิลด์ key และ ค่าของข้อมูลจะ คืนด้วยฟิลด์ value - ฟังก์ชัน list ทำงานเช่นเดียวกับ each() แต่จะแยก array ออกจาก กันและเก็บในตัวแปรที่กำหนด - ฟังก์ชัน reset ใช้สำหรับย้ายพอยน์เตอร์ ไปชี้ที่ค่าแรกของ array

  31. Array ที่มี index เป็นตัวอักษร การสร้าง array ใช้ฟังก์ชัน 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>"; }

  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>"; }

  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>"; }

  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>"; }

  35. การใช้งาน string การตัดช่องว่างในสตริง - trim() สำหรับตัดช่องว่างทั้งหมดที่อยู่ด้านหน้าและด้านหลัง ของสตริงทิ้งไป - ltrim() ตัดช่องว่างที่อยู่ด้านหน้า(ทางซ้าย) ทิ้งไป - chop() ตัดช่องว่างที่อยู่ด้านหลัง (ทางขวา) ทิ้งไป

  36. trim() <? $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>"; ?>

  37. ltrim() <? $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>"; ?>

  38. chop() <? $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>"; ?>

  39. nl2br() เปลี่ยน \n ให้เป็น <br> <? $str =" Computer\n Science\nChiang Mai\n\n"; print "String = ".$str."<br>"; echo “use nl2br() <br>"; echo "String = ".nl2br($str); ?>

  40. การแสดงข้อมูลทางจอภาพการแสดงข้อมูลทางจอภาพ การแสดงตัวเลขโดยใช้คำสั่ง echo นั้น จะไม่ สามารถจัดรูปแบบได้ แต่ถ้าใช้ฟังก์ชัน printf() และ sprintf() จะสามารถจัดรูปแบบได้ <? $str =" Computer\n Science\nChiang Mai\n\n"; printf("String = %s<br>",$str); print “<em>use nl2br() </em><br>"; echo "String = ".nl2br($str); ?>

  41. ค่า args สำหรับการแสดงรูปแบบ

  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>";

  43. การเปลี่ยนรูปแบบของตัวอักษรการเปลี่ยนรูปแบบของตัวอักษร ตัวอย่างคำสั่ง $str= “Please ENTER your name”; strtoupper($str); strtolower($str); ucfirst($str); ucwords($str);

  44. การใช้ Regular Expression regular expression ใช้สำหรับการเปรียบเทียบและ แทนที่ข้อมูลแบบสตริง ซึ่งคล้ายกับการใช้ฟังก์ชัน เปรียบเทียบและแทนที่ของสตริง แต่ regular expression จะสามารถกำหนดรูปแบบของสตริงได้มากกว่า เพื่อใช้ใน การค้นหาสตริงที่ใกล้เคียงกันได้

  45. ฟังก์ชันสำหรับตรวจสอบค่าสตริงฟังก์ชันสำหรับตรวจสอบค่าสตริง ฟังก์ชันการค้นหาสตริงย่อย - ereg() ใช้สำหรับตรวจสอบข้อความและตัวอักษร ฟังก์ชันจะตี ความตัวอักษรพิมพ์ใหญ่และตัวอักษรพิมพ์เล็กแตกต่างกัน รูปแบบ int ereg(string pattern, string string) - eregi() ใช้สำหรับการตรวจสอบข้อความและตัวอักษร โดยจะ ตีความตัวอักษรพิมพ์ใหญ่และตัวพิมพ์เล็กเหมือนกัน รูปแบบ int eregi(string pattern, string string)

  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>"; ?>

  47. ฟังก์ชันการแทนที่สตริงย่อย - ereg_replace() ใช้สำหรับแทนที่ข้อความและตัวอักษร ฟังก์ชันจะตี ความตัวอักษรพิมพ์ใหญ่และตัวอักษรพิมพ์เล็กแตกต่างกัน รูปแบบ string ereg_replace(string pattern, string replace,string string) - eregi_replace() ใช้สำหรับแทนที่ข้อความและตัวอักษร โดยจะตีความตัว อักษรพิมพ์ใหญ่และตัวพิมพ์เล็กเหมือนกัน รูปแบบ string eregi_replace(string pattern, string replace,string string)

  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); ?>

  49. ฟังก์ชันการแบ่งสตริง - split() ใช้สำหรับแบ่งสตริงแล้วไปเก็บไว้ในอาร์เรย์ รูปแบบ array split(string pattern, string search, int [max]) - spliti() ใช้สำหรับแบ่งสตริง โดยจะตีความตัวอักษรพิมพ์ใหญ่และ ตัวพิมพ์เล็กเหมือนกัน รูปแบบ array spliti(string pattern, string search, int [max])

  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>"; ?>

More Related