1 / 34

ฟังก์ชัน

ฟังก์ชัน. เสรี ชิโนดม seree@buu.ac.th. นิยาม. ฟังก์ชันคือส่วนของโปรแกรมที่ทำงานเสร็จสิ้นภายในตัวเอง มีลักษณะเหมือนกับโปรแกรมย่อยสับรูทีน (subroutine) ภาษา PHP มีฟังก์ชันที่เตรียมให้ใช้งาน 2 ชนิด คือ

arvin
Download Presentation

ฟังก์ชัน

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. ฟังก์ชัน เสรี ชิโนดม seree@buu.ac.th

  2. นิยาม • ฟังก์ชันคือส่วนของโปรแกรมที่ทำงานเสร็จสิ้นภายในตัวเอง มีลักษณะเหมือนกับโปรแกรมย่อยสับรูทีน (subroutine) • ภาษา PHP มีฟังก์ชันที่เตรียมให้ใช้งาน 2 ชนิด คือ • Library Function คือ ฟังก์ชันที่ภาษา PHP สร้างไว้เรียบร้อยแล้ว สามารถเรียกใช้ได้ทันที • ฟังก์ชันที่ผู้ใช้โปรแกรมเขียนขึ้นเอง (User-defined functions) PHP Programming

  3. ฟังก์ชันที่ผู้ใช้โปรแกรมเขียนขึ้นเอง (User-defined functions) การสร้างฟังก์ชันขึ้นใช้เอง มีรูปแบบดังนี้ function function_name ([parameterList]) { รายการชุดข้อความคำสั่ง   ... [return ค่าที่ต้องการส่งกลับ] } PHP Programming

  4. ตัวอย่าง ไฟล์ func-1.php <html> <body> <font size=4 face ='arial'> <? function welcome() { print ("<H1 align = center> Welcome To Computer Science</H1>" ); } ?> <H1> This is a test</H1> <? welcome(); ?> </body> </html> PHP Programming

  5. การเรียกใช้ฟังก์ชัน • การเรียกใช้ฟังก์ชันโดยไม่มีการส่งพารามิเตอร์ function_name(); • การเรียกใช้ฟังก์ชันโดยมีการส่งพารามิเตอร์ function_name(para1,para2,…); PHP Programming

  6. การเรียกใช้ฟังก์ชัน <? //func-2.php function bold( $what ){ print("<B>$what</B>"); } bold( "HELLO" ); ?> PHP Programming

  7. การส่งค่ากลับของฟังก์ชันการส่งค่ากลับของฟังก์ชัน • ฟังก์ชันจะให้ค่ากลับคืนหรือไม่ก็ได้ถ้าต้องการให้ค่ากลับคืนจากการทำงานของฟังก์ชัน จะใช้คำสั่ง return • การเรียกใช้ฟังก์ชัน ทำได้โดยการอ้างถึงชื่อของฟังก์ชันที่ต้องการโดยที่คำสั่งที่เรียกใช้นั้นจะอยู่ก่อนหรือหลังฟังก์ชันที่ถูกเรียกก็ได้ PHP Programming

  8. การส่งค่ากลับของฟังก์ชันการส่งค่ากลับของฟังก์ชัน ตัวอย่าง ไฟล์ func-3.php <? //func-2.php function area( $b , $h ){ return (0.5)*$b*$h; } print("Area is ".area(3,4) ); ?> PHP Programming

  9. <HTML> <HEAD> <TITLE>Figure 4-2</TITLE> </HEAD> <BODY> <? function printBold($inputText) { print("<B>" . $inputText . "</B>"); } print("This Line is not Bold<BR>\n"); printBold("This Line is Bold"); print("<BR>\n"); print("This Line is not Bold<BR>\n"); ?> </BODY> </HTML> PHP Programming

  10. การผ่านค่าพารามิเตอร์ให้กับฟังก์ชันการผ่านค่าพารามิเตอร์ให้กับฟังก์ชัน การผ่านค่าไปยังฟังก์ชันมี 2 วิธี 1. Call by values ตัวแปรรับค่ามาแล้วเกิดการเปลี่ยนแปลงค่าจะไม่มีการส่งกลับคืน 2. Call by reference การส่งข้อมูลในกรณีนี้ตัวแปรพารามีเตอร์ที่รับค่าจะต้องนำหน้าด้วยเครื่องหมาย “&” กรณีนี้จะทำให้ตัวแปรที่ทำหน้าที่ส่งค่าและตัวแปรที่ทำหน้าที่รับค่าอ้างอิงที่อยู่ของข้อมูลเดียวกัน เมื่อมีการเปลี่ยนแปลงค่าของตัวแปรพารามีเตอร์ในฟังก์ชันก็จะมีผลทำให้ค่าของตัวแปรอาร์กิวเมนต์ที่ส่งไปให้โปรแกรมเปลี่ยนไปด้วย PHP Programming

  11. ตัวอย่าง ไฟล์ func-4.php <? //Call by values function one ( $parameter ) { $parameter++; } $a = 10 ; one( $a ) ; echo “ a = $a <BR>” ; ?> ผลลัพธ์ของโปรแกรมคือ a = 10 PHP Programming

  12. ตัวอย่าง ไฟล์ func-5.php <? //Call by reference function one ( &$parameter ) { $parameter++; } $a = 10 ; one( $a ) ; echo “ a = $a <BR>” ; ?> ผลลัพธ์ของโปรแกรมคือ a = 11 PHP Programming

  13. ตัวอย่าง ไฟล์ func-6.php การสลับค่าของตัวแปรสองตัวด้วยฟังก์ชัน swap() <? function swap(&$a, &$b) { $t = $a; $a = $b; $b = $t; } $x=10; $y=20; print ("BEFORE Interchange <BR>"); echo "x=",$x,",y=",$y,"<BR>\n"; swap($x,$y); print ("AFTER Interchange <BR>"); echo "x=",$x,",y=",$y,"<BR>\n"; ?> PHP Programming

  14. มีข้อสงเกตอยู่ว่า การใช้ call-by-referenceไม่จำเป็นต้องทำตอนนิยามฟังก์ชันเท่านั้นแต่อาจจะทำตอนผ่านตัวแปรเมื่อเรียกใช้งานจริง ตัวอย่างเช่น <? function swap($a, $b) { $t = $a; $a = $b; $b = $t; } $x=10; $y=20; print ("BEFORE Interchange <BR>"); echo "x=",$x,",y=",$y,"<BR>\n"; swap(&$x,&$y); print ("AFTER Interchange <BR>"); echo "x=",$x,",y=",$y,"<BR>\n"; ?> PHP Programming

  15. การกำหนดค่าเริ่มต้นให้กับตัวแปรพารามิเตอร์การกำหนดค่าเริ่มต้นให้กับตัวแปรพารามิเตอร์ เราสามารถกำหนดค่าเริ่มต้นให้กับตัวแปรทำให้มีประสิทธิภาพมากขึ้น ตัวอย่าง <? function font( $str_text, $str_color = 'blue', $int_size = 2 ) { echo "<font size=\"$int_size\" color=\"$str_color\">$str_text</a>"; } ?> <? font('This line is blue - size 2.'); ?> <br> <? font('This line is red - size 2.', 'red'); ?><br> <? font('This line is green - size 4', 'green', 4); ?><br> This line is blue – size 2. This line is blue – size 2. This line is blue – size 4. PHP Programming

  16. ขอบเขตของตัวแปร มี 2 แบบ คือ 1. Global scopeเมื่อกำหนดตัวแปรแบบนี้แล้วสามารถเรียกใช้ได้ทุกส่วนของโปรแกรมและค่าของตัวแปรนั้นยังอยู่จนกว่าโปรแกรมนั้นจะจบหรือหยุดทำงาน 1.1 โดยใช้คำสั่ง global 1.2 โดยผ่านตัวแปรอาร์เรย์ชื่อ $GLOBAL PHP Programming

  17. <? //funcscope1.php $position = "m" ; function change_pos(){ $position = "b"; } change_pos(); echo ("$position"); //print "m" ?> PHP Programming

  18. <? //funcscope2.php $position = "m" ; function change_pos(){ global $position ; $position = "b"; } change_pos(); echo ("$position"); // print "b" ?> PHP Programming

  19. การใช้ตัวแปรแบบ global ภายในฟังก์ชัน 2. Local Scope คือตัวแปรที่กำหนดขึ้นใช้ภายในฟังก์ชันนั้นๆเท่านั้น เมื่อออกจากส่วนของโปรแกรมนั้นไปแล้วตัวแปรนั้นจะหายไปและตัวแปรที่กำหนดในฟังก์ชันหนึ่งไม่สามารถเรียกใช้อีกจากฟังก์ชันหนึ่งได้ PHP Programming

  20. การกำหนดตัวแปรแบบ static ภายในฟังก์ชัน ตัวแปรภายในฟังก์ชันจะสามารถเก็บค่าไว้ได้ตลอดเวลาโดยไม่สูญหายไป function MyFunc() {   static $num_func_calls = 0;   echo "my function\n";   return ++$num_func_calls; } PHP Programming

  21. ฟังก์ชันเรียกใช้ฟังก์ชัน (Nesting Function Calls) ตัวอย่าง <?php function increment_by_two( $int_parameter ) { $int_parameter += 2; return($int_parameter); } มีต่อ PHP Programming

  22. function increment_by_three( $int_parameter ) { $int_parameter++; // make a nested function call to increment_by_two(). return( increment_by_two($int_parameter) ); } $a = increment_by_three( 34 ); echo "a = $a<br>"; ?> ผลลัพธ์ของโปรแกรมคือ a = 37 PHP Programming

  23. การสร้างฟังก์ชันแบบเรียกตัวเอง (recursive function) ฟังก์ชันที่มีคำสั่งภายในฟังก์ชันนั้นเรียกใช้ฟังก์ชันตัวเองแบบวนลูป ตัวอย่าง การหาค่าแฟกทอเรียล n! function factorial ($n) {   if ( ($n == 0) || ($n == 1) )    return 1;   else     return $n*factorial($n-1); } PHP Programming

  24. ตัวอย่างที่ 5-10fibonacci.inc การหาค่า fibonacci number <? function fibonacci( $var ) { // the fibonacci series is only defined for // positive values. if ($var < 0) { return( 0 ); } // the first two elements in the series are // defined as zero and one and don't need recursion. if ($var < 2) { return( $var ); } // use recursion to find the previous two elements // in the series. return( fibonacci($var-1) + fibonacci($var-2) ); } ?> PHP Programming

  25. โปรแกรมที่เรียกใช้ โปรแกรมที่เรียกใช้คือ <? // include the fibonacci function in this script require (‘fibonacci.inc’) ; // call the fibonacci function with a parameter of four. $n = Fibonacci ( 4 ) ; // display the result of the fibonacci call. Echo “$n <BR>” ; ?> PHP Programming

  26. การผ่านค่ากลับคืนมากกว่าหนึ่งจากฟังก์ชันการผ่านค่ากลับคืนมากกว่าหนึ่งจากฟังก์ชัน • โดยปรกติแล้วเราไม่สามารถผ่านค่ากลับคืนจากฟังก์ชันได้มากกว่าหนึ่งแต่อย่างไรก็ตาม ยังมีวิธีการหนึ่งที่ช่วยแก้ปัญหาดังกล่าวได้ วิธีนี้คือเก็บค่าต่างๆที่ต้องการจะใช้เป็นค่ากลับคืนไว้ใน array แล้วใช้ arrayนั้นเป็นค่ากลับคืน และผู้เรียกใช้ฟังก์ชันสามารถใช้ฟังก์ชัน list()อ่านค่าเหล่านั้นได้ ตัวอย่างเช่น function foobar() {    return array ("foo", "bar", 0xff); } list ($foo, $bar, $num) = foobar(); echo "$foo $bar $num <BR>\n"; ผลลัพธ์ของโปรแกรมคือ foo bar 255 PHP Programming

  27. <HTML> <HEAD> <TITLE>Figure 4-7: Dynamically Calling a Function</TITLE> </HEAD> <BODY> <? function write($text) { print($text); } function writeBold($text) { print("<B>$text</B>"); } PHP Programming

  28. $myFunction = "write"; $myFunction("Hello!"); print("<BR>\n"); $myFunction = "writeBold"; $myFunction("Goodbye!"); print("<BR>\n"); ?> </BODY> </HTML> PHP Programming

  29. การใช้คำสั่ง include และ require คำสั่งทั้งสองมีไว้เพื่อแทรกเนื้อหาจากไฟล์อื่นที่ต้องการโดย คำสั่งrequire จะอ่านเพียงครั้งเดียว คือไฟล์แรกและจะแทรกไฟล์นี้เท่านั้นไปตามจำนวนครั้งที่วนลูป ในขณะที่ includeสามารถอ่านได้ไฟล์ต่างๆกันตามจำนวนครั้งที่ต้องการ PHP Programming

  30. ตัวอย่าง <? $filename[]="file1.inc"; $filename[]="file2.inc"; for ($i = 0; $i < 2; $i++) { include $filename[$i]; } ?> ไฟล์ file1.inc Hello world 1<BR> ไฟล์ file2.inc Hello world 2<BR> PHP Programming

  31. การอ่านตัวแปรจากภายนอกที่ได้จากการ Web browser โดยวิธี GET หรือ POST การส่งและรับข้อมูลจะหว่าง Web browser กับ Script ที่เขียนขึ้นมา ตัวอย่าง การใช้ Post <form action="login.php3" method="post"> <table>  <tr><td>login:</td> <td><input type="text" name="login"></td> </tr><br> มีต่อ PHP Programming

  32. <tr><td>password:</td> <td><input type="text“name="password"></td> </tr><br> </table> <p><input type="submit"> </form> PHP Programming

  33. ตัวอย่าง ไฟล์ login.php3 <HTML> <HEAD><TITLE> Result </TITLE></HEAD> <BODY> <P> Your login = <? echo "$login" ?> <BR> Your password = <? echo "$password"; ?> </BODY> </HTML> PHP Programming

  34. การตรวจชนิดของ web browser ตัวอย่าง  function getBrowserName() {   global $HTTP_USER_AGENT;   $browser=strtoupper($HTTP_USER_AGENT);   if (strstr($browser,"MSIE."))     return "MS Internet Explorer";   else if (strstr($browser,"MOZILLA"))     return "Netscape";   else     return "";  } PHP Programming

More Related