61
1 ฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟ PHP echo, print ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ echo ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟ (variable functions) ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟ ฟฟฟฟ/ฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟ ฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟฟ

PHP Tutorial (funtion)

  • Upload
    -

  • View
    183

  • Download
    5

Embed Size (px)

Citation preview

Page 1: PHP Tutorial (funtion)

1

ฟั�งก์�ชั�นต่างๆ ใน PHP echo, print

ไม่ได้�เป็�นฟั�งก์�ชั� �นที่��แที่�จริ�ง จ�งสาม่าริถใชั�วงเล็"บ หริ%อไม่ใชั�ก์"ได้�

echo ไม่สาม่าริถใชั�ได้�ในฟั�งก์�ชั�นต่�วแป็ริ (variable functions)

ฟั�งก์�ชั� �นต่�วแป็ริ คื%อต่�วแป็ริที่��ม่�คืาเป็�นชั%�อฟั�งก์�ชั� �น หาก์ต่�วแป็ริน�(นม่�วงเล็"บ

เป็)ด้/ป็)ด้ต่อที่�าย ก์"จะสาม่าริถที่,าหน�าที่��ได้�เหม่%อนฟั�งก์�ชั� �น

Page 2: PHP Tutorial (funtion)

2

ฟั�งก์�ชั�นต่างๆ ใน PHP echo, print

Demo

<? #ecpr.php

function TestSet($a){

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

}

$iTestSet = "TestSet";

$iTestSet(5);

?>

Page 3: PHP Tutorial (funtion)

3

ฟั�งก์�ชั�นต่างๆ ใน PHP settype()

ใชั�ในก์าริก์,าหนด้ ต่�วแป็ริ แล็ะ data type ให�ก์�บต่�วแป็ริ ริ-ป็แบบ

settype($varname, “datatype”);

Demo

<? #settype.php

settype($i, "integer");

?>

Page 4: PHP Tutorial (funtion)

4

ฟั�งก์�ชั�นต่างๆ ใน PHP gettype()

ใชั�เพื่%�อด้-ป็ริะเภที่ของ data type ของต่�วแป็ริ ริ-ป็แบบ

gettype($varname);

Demo

<? #settype.php

settype($i, "integer");

print(gettype($i));

?>

Page 5: PHP Tutorial (funtion)

5

ฟั�งก์�ชั�นต่างๆ ใน PHP isset()

ใชั�เพื่%�อต่ริวจสอบด้-วาต่�วแป็ริน�(นถ-ก์สริ�างหริ%อไม่ (จริ�ง = 1, เที่"จ = blank)

ริ-ป็แบบ isset($varname);

Demo <? #isset.php

settype($i, "integer");print(gettype($i));print("<br>".isset($a));print("<br>".isset($i));

?>

Page 6: PHP Tutorial (funtion)

6

Introduction to HTML เบ%(องต่�นก์�บก์าริเข�ยน HTML

<html>

<head>

<!-- comment for HTML -->

</head>

<body>

</body>

</html>

Page 7: PHP Tutorial (funtion)

7

Introduction to HTML เบ%(องต่�นก์�บก์าริเข�ยน HTML

<html><head><!-- comment for HTML --></head><!-- comment --><body>

<h1>php with html</h1><?php

//---print "PHP with HTML";

?><form>

<?php//---

?></form></body></html>

Page 8: PHP Tutorial (funtion)

8

Introduction to HTML เบ%(องต่�นก์�บก์าริเข�ยน HTML

<!--comment for HTML-->

<html>

<head>

<title>MyHTML</title>

</head>

<body>

สวนแสด้งผล็ข�อม่-ล็</body>

</html>

Page 9: PHP Tutorial (funtion)

9

Introduction to HTML

เบ%(องต่�นก์�บก์าริเข�ยน HTMLbackground ใชั�ก์,าหนด้ไฟัล็�ริ-ป็ภาพื่ที่��จะแสด้งเป็�น background

bgcolor ใชั�ก์,าหนด้ส�ที่��จะใชั�เป็�น background

text ใชั�ก์,าหนด้ส�ที่��จะใชั�เป็�น foreground

events onLoad, onUpload

Page 10: PHP Tutorial (funtion)

10

Introduction to HTML form

Action ใชั�ริะบ2 URL ป็ล็ายที่างเม่%�อที่,าก์าริ submitMethod ริะบ2ว�ธี�ก์าริสงข�อม่-ล็ ม่� 2 แบบ

Get ข�อม่-ล็ถ-ก์สงต่อไป็ก์�บ URL แล็ะแสด้งผล็ในชัอง URL ของ browser

POST ข�อม่-ล็ที่��ถ-ก์สงจะไม่แสด้งให�เห"นในชัอง URLName ชั%�อฟัอริ�ม่

ป็ริะก์อบด้�วย control<input><textarea>

Page 11: PHP Tutorial (funtion)

11

Introduction to HTML input ม่� type เป็�น

text password

button reset submit radio

checkbox hidden

Page 12: PHP Tutorial (funtion)

12

Introduction to HTML input

type ใชั�ก์,าหนด้ริ-ป็แบบของก์าริริ�บข�อม่-ล็จาก์ user

name ชั%�อที่��ต่� (งให�ก์�บชัองที่างที่��ก์,าหนด้ริ-ป็แบบในก์าริริ�บข�อม่-ล็Value คืาที่��จะให�แสด้งผล็ผานชัองที่างที่��ก์,าหนด้ริ-ป็แบบในก์าริริ�บ ข�อม่-ล็น�(นๆ

Page 13: PHP Tutorial (funtion)

13

Introduction to HTML input - text

name ชั%�อที่��ต่� (งให�ก์�บชัองริ�บข�อม่-ล็value คืาที่��จะให�แสด้งผล็ในชัองริ�บข�อม่-ล็size คืวาม่ก์ว�างของชัองริ�บข�อม่-ล็maxlength จ,านวนต่�วอ�ก์ษริที่��ก์ริอก์ได้�ส-งส2ด้ในชัองริ�บข�อม่-ล็events เชัน onChange, onKeyUp

Page 14: PHP Tutorial (funtion)

14

Introduction to HTML input - password

name ชั%�อที่��ต่� (งให�ก์�บชัองริ�บข�อม่-ล็value คืาที่��จะให�แสด้งผล็ในชัองริ�บข�อม่-ล็ (encrypted)

size คืวาม่ก์ว�างของชัองริ�บข�อม่-ล็maxlength จ,านวนต่�วอ�ก์ษริที่��ก์ริอก์ได้�ส-งส2ด้ในชัองริ�บข�อม่-ล็events เชัน onChange, onKeyUp

Page 15: PHP Tutorial (funtion)

15

Introduction to HTML input – button, reset, submit

name ชั%�อที่��ต่� (งให�ก์�บชัองริ�บข�อม่-ล็value คืาที่��จะให�แสด้งผล็บนป็25ม่events เชัน onChange, onKeyUp

Page 16: PHP Tutorial (funtion)

16

Introduction to HTML input – radio, checkbox

name ชั%�อที่��ต่� (งให�ก์�บชัองริ�บข�อม่-ล็value คืาที่��จะให�เก์"บก์�บ name เม่%�อม่�ก์าริเล็%อก์ที่��ป็25ม่checked ก์,าหนด้ให�ชัองน�(แสด้งวาถ-ก์เล็%อก์events เชัน onClick

Page 17: PHP Tutorial (funtion)

17

Introduction to HTML input – hidden

name ชั%�อที่��ต่� (งให�ก์�บ hidden

value คืาที่��จะให�เก์"บก์�บ name เม่%�อม่�ก์าริ submit

Page 18: PHP Tutorial (funtion)

18

Introduction to HTML table

border ใชั�ริะบ2ขนาด้เส�นขอบของต่าริางwidth ก์,าหนด้คืวาม่ก์ว�างต่าริางheight ก์,าหนด้คืวาม่ส-งต่าริาง

Page 19: PHP Tutorial (funtion)

19

Introduction to HTML tr, td, th

align ก์,าหนด้ก์าริจ�ด้วางในแนวนอนvalign ก์,าหนด้ก์าริจ�ด้วางในแนวต่�(งbgcolor ใชั�ก์,าหนด้ส�พื่%(นหล็�งwidth ก์,าหนด้คืวาม่ก์ว�างต่าริางheight ก์,าหนด้คืวาม่ส-งต่าริาง

Page 20: PHP Tutorial (funtion)

20

Introduction to HTMLDemo – table.htm<!-- table.htm --><html><head></head><body>

<table border="1"> <!-- tag table opened here --><tr> <!-- tag tr for row opened here -->

<th>No.</th> <!-- tag th for table header --><th>col1</th><th>col2</th>

</tr> <!-- tag th for table header --><tr>

<td>row1</td> <!-- tag td for column detail --><td>table detail row 1 column 1</td><td>table detail row 1 column 2</td>

</tr><tr>

<td>row2</td><td>table detail row 2 column 1</td><td>table detail row 2 column 2</td>

</tr></table> <!-- tag table closed here -->

</body></html>

Page 21: PHP Tutorial (funtion)

21

Introduction to HTMLDemo – frminput.htm<!-- frminput.htm --><html><!-- frminput.htm--><head><title>Input form</title></head><body>

<form name="frmInput" method="post" action="getInfo.php">Student Name :<input type=text name=txtName><br>Student ID :<input type=text name=txtID><br>Sex : <select name=selSex>

<option value=M>ชัาย</option><option value=W>หญิ�ง</option>

</select>Score :<input type=text name=txtScore><br><input type=submit value=Submit><br>

</form></body></html>

Page 22: PHP Tutorial (funtion)

22

Introduction to HTMLDemo – getinfo.php <? #getinfo.php

echo "<center><h3>";print "ริายงานเก์ริด้ น.ศ. ว�ชัาก์าริศ�ก์ษาเฉพื่าะที่างด้�านคือม่พื่�วเต่อริ�<br>";print "ชั%�อ น.ศ. $txtName<br>";print "ริห�ส น.ศ. $txtID<br>";switch($selSex){

case "M" : $selSex="ชัาย"; break;default : $selSex="หญิ�ง"; break;

}print "เพื่ศ $selSex<br>";if($txtScore < 50){

$getGrade = "D";}elseif($txtScore < 65){

$getGrade = "C";}elseif($txtScore < 80){

$getGrade = "B";}else{

$getGrade = "A";}print "เก์ริด้ที่��ได้� $getGrade<br>";echo "</h3></center>";

?>

Page 23: PHP Tutorial (funtion)

23

Introduction to HTMLDemo – frmInput02.htm<html>

<head></head>

<body>

<form name="frmInput" method="post" action="getInfo02.php">

<table>

<tr>

<td align="right">Student Name:</td>

<td><input type=text name=txtName></td>

</tr>

<tr>

<td align="right">Student ID:</td>

<td><input type=text name=txtID></td>

</tr>

Page 24: PHP Tutorial (funtion)

24

Introduction to HTMLDemo – frmInput02.htm (ต่อ) <tr>

<td align="right">Sex:</td><td>

<select name=selSex><option value=M>ชัาย</option><option value=F>หญิ�ง</option>

</select></td>

</tr><tr>

<td align="right">Score:</td><td><input type=text name=txtScore></td>

</tr><tr>

<td colspan="2" align="center"><input type=submit value=Submit></td></tr></table>

</form></body></html>

Page 25: PHP Tutorial (funtion)

25

Introduction to HTMLDemo – getInfo2.php <? #getinfo02.php

echo "<center><h3>";print "ริายงานเก์ริด้ น.ศ. ว�ชัาก์าริศ�ก์ษาเฉพื่าะที่างด้�านคือม่พื่�วเต่อริ�

<br>";print " ชั%�อ น.ศ. $txtName<br>";print " ริห�ส น.ศ. $txtID<br>";switch($selSex){

case "M“ :$selSex=“ชัาย"; break;default :$selSex=“หญิ�ง";break;

}

Page 26: PHP Tutorial (funtion)

26

Introduction to HTMLDemo – getInfo2.php (ต่อ) print " เพื่ศ $selSex<br>";

if($txtScore < 50)$getGrade = "D";elseif($txtScore < 65)$getGrade = "C";elseif($txtScore < 80)$getGrade = "B";else$getGrade = "A";print " เก์ริด้ที่��ได้�ริ�บ $getGrade<br>";echo "<h3></center>";

?>

Page 27: PHP Tutorial (funtion)

27

ฟั�งก์�ชั�นต่างๆ ใน PHPsubstr()

ฟั�งก์�ชั� �นที่��ใชั�ในก์าริต่�ด้ต่�วอ�ก์ษริหริ%อข�อคืวาม่ออก์จาก์ข�อคืวาม่เด้�ม่ ริ-ป็แบบ substr(string string, int start[, int length]);

string ข�อคืวาม่ที่��ต่�องก์าริน,าม่าต่�ด้ start ต่,าแหนงเริ��ม่ต่�นของต่�วอ�ก์ษริที่��ต่�องก์าริต่�ด้ ม่�คืาบวก์หริ%อล็บก์"ได้�

โด้ยต่�วอ�ก์ษริต่�วแริก์เริ��ม่ที่��คืา 0 length จ,านวนต่�วอ�ก์ษริที่��ต่�องก์าริต่�ด้

Page 28: PHP Tutorial (funtion)

28

ฟั�งก์�ชั��นต่างๆ ใน PHPDemo – substr.php

<? //substr.php

$text = "integer is number";

print(substr($text, 0, 7));

?>

Page 29: PHP Tutorial (funtion)

29

ฟั�งก์�ชั�นต่างๆ ใน PHPsubstr_replace()

ฟั�งก์�ชั��นที่��ใชั�ในก์าริแที่ริก์หริ%อแที่นที่��ต่�วอ�ก์ษริหริ%อข�อคืวาม่ในข�อคืวาม่เด้�ม่ ริ-ป็แบบ substr_replace(string string, string replacement,

int start[, int length]);

string ข�อคืวาม่ที่��ต่�องก์าริน,าต่�วอ�ก์ษริหริ%อข�อคืวาม่อ%�นม่าแที่ริก์หริ%อแที่นที่��ใน

ข�อคืวาม่ replacement ข�อคืวาม่ที่��ต่�องก์าริน,าม่าแที่ริก์หริ%อแที่นที่�� start ต่,าแหนงเริ��ม่ต่�นของต่�วอ�ก์ษริที่��ต่�องก์าริต่�ด้ ม่�คืาบวก์หริ%อล็บก์"ได้�

โด้ยต่�วอ�ก์ษริต่�วแริก์เริ��ม่ที่��คืา 0 length จ,านวนต่�วอ�ก์ษริที่��ต่�องก์าริน,าม่าแที่ริก์หริ%อแที่นที่�� ม่�คืาบวก์หริ%อล็บก์"ได้�

Page 30: PHP Tutorial (funtion)

30

ฟั�งก์�ชั��นต่างๆ ใน PHPDemo – substrrp.php <?php #substrrp.php

$text = "integer is number";$newtext = substr($text, 0, 10);print($newtext."<br>");$newtext = substr_replace($newtext, " not float", 11);print($newtext."<br>");$newtext = substr_replace($text, " not float", 11);print($newtext."<br>");$newtext = substr_replace($text, " not float", 11,0);print($newtext."<br>");?>

Page 31: PHP Tutorial (funtion)

31

ฟั�งก์�ชั�นต่างๆ ใน PHP str_replace()

ฟั�งก์�ชั��นที่��ใชั�ในก์าริแที่นที่��ต่�วอ�ก์ษริหริ%อข�อคืวาม่ในข�อคืวาม่เด้�ม่ ริ-ป็แบบ mixed str_replace(mixed search, mixed replace,

mixed subject[, int &count]);

search คื,า/หริ%อข�อคืวาม่ที่��ต่�องก์าริให�ถ-ก์แที่นที่�� replace คื,า/หริ%อข�อคืวาม่ที่��ต่�องก์าริน,าม่าแที่นที่�� subject ข�อคืวาม่หล็�ก์ count ต่�วแป็ริส,าหริ�บริ�บคืาจ,านวนคื,าหริ%อสวนของข�อคืวาม่ที่��ถ-ก์แที่นที่��

ซึ่��งถ-ก์สงผานโด้ยว�ธี�ก์าริอ�างอ�ง (pass by reference) ซึ่��งจะต่�องม่�

เคืริ%�องหม่าย & อย-หน�าต่�วแป็ริ

Page 32: PHP Tutorial (funtion)

32

ฟั�งก์�ชั��นต่างๆ ใน PHPstr_replace()

Demo – strrp.php

<?php #strrp.php

$oldstr = "integer is number";

$newstr = str_replace("integer", "float", $oldstr);

print($newstr."<br>");

?>

Page 33: PHP Tutorial (funtion)

33

ฟั�งก์�ชั�นต่างๆ ใน PHP strpos()

ฟั�งก์�ชั� �นที่��ใชั�ในก์าริหาต่,าแหนงของต่�วอ�ก์ษริหริ%อข�อคืวาม่ในข�อคืวาม่เด้�ม่ ริ-ป็แบบ int strpos(string haystack, string needle [, int offset]);

haystack ข�อคืวาม่ที่��ต่�องก์าริต่ริวจสอบ needle ต่�วอ�ก์ษริหริ%อข�อคืวาม่ที่��ต่�องก์าริคื�นหา offset ต่,าแหนงที่��ให�เริ��ม่ต่�นหา อ�ก์ษริต่�วแริก์น�บเป็�น 0

เป็�น optional parameter

ผล็ล็�พื่ธี�ที่��ได้�จะเป็�นต่,าแหนงแริก์ที่��พื่บต่�วอ�ก์ษริหริ%อข�อคืวาม่ที่��คื�นหา

Page 34: PHP Tutorial (funtion)

34

ฟั�งก์�ชั��นต่างๆ ใน PHPstrpos()

Demo – strpos.php <?php #strpos.php

$email = "[email protected]";

$name = substr($email, 0, strpos($email, "@"));

print("Name : ".$name."<br>");

$domain = substr($email, strpos($email, "@")+1, 11);

print("Domain : ".$domain."<br>");

?>

Page 35: PHP Tutorial (funtion)

35

ฟั�งก์�ชั��นต่างๆ ใน PHPDEMO - frminputstrpos2.htm

<!-- frminputstrpos2.htm --><html><!-- frminputstrpos2.htm--><head><title>Input form</title></head><body></body>

<form name="frmInput" method="post" action="strpos2.php">E-mail :<input type=text name=txtEMail><br>Year :<input type=text name=txtID><br><input type=submit value=Submit><br>

</form></html>

Page 36: PHP Tutorial (funtion)

36

ฟั�งก์�ชั��นต่างๆ ใน PHPstrpos()Demo – strpos2.php <?php #strpos2.php

print "<h1>";print “บริริจ2เข�าที่,างานป็; 25".substr($txtID, 2, 2)."<br>";print “ใชั� e-Mail ชั%�อ : ";print substr($txtEMail, 0, strpos($txtEMail, "@"));print "<br>";print "Domain ของ : ".substr($txtEMail, strpos($txtEMail, "@")+1);print "<br>";print "</h1>";

?>

Page 37: PHP Tutorial (funtion)

37

ฟั�งก์�ชั�นต่างๆ ใน PHP strrpos()

ฟั�งก์�ชั��นที่��ใชั�ในก์าริหาต่,าแหนงของต่�วอ�ก์ษริหริ%อข�อคืวาม่ในข�อคืวาม่เด้�ม่

เชันเด้�ยวก์�บ strpos() แต่จะคื�นหาเริ��ม่จาก์ต่�วส2ด้ที่�าย ริ-ป็แบบ int strrpos(string haystack, string needle [, int offset]);

haystack ข�อคืวาม่ที่��ต่�องก์าริต่ริวจสอบ needle ต่�วอ�ก์ษริหริ%อข�อคืวาม่ที่��ต่�องก์าริคื�นหา offset ต่,าแหนงที่��ให�เริ��ม่ต่�นหา อ�ก์ษริต่�วแริก์น�บเป็�น 0

เป็�น optional parameter

Page 38: PHP Tutorial (funtion)

38

ฟั�งก์�ชั��นต่างๆ ใน PHPstrrpos()Demo – strrpos.php

<?php #strrpos.php

$email = "[email protected]";

print strpos($email, "a")."<br>";

print strrpos($email, "a")."<br>";

?>

Page 39: PHP Tutorial (funtion)

39

ฟั�งก์�ชั�นต่างๆ ใน PHP strlen()

ฟั�งก์�ชั��นที่��ใชั�ในก์าริหาจ,านวนต่�วอ�ก์ษริที่�(งหม่ด้ในข�อคืวาม่ ริ-ป็แบบ int strlen(string, string); DEMO strlen()<?php #strlen.php

$email = "[email protected]";print("email length ".strlen($email)."<br>");$name = substr($email, 0, strpos($email, "@"));print("name length ".strlen($name)."<br>");$domain = substr($email, strpos($email, "@")+1);print("domain length ".strlen($domain)."<br>");

?>

Page 40: PHP Tutorial (funtion)

40

ฟั�งก์�ชั�นต่างๆ ใน PHP ltrim(), rtrim(), trim(), chop()

เป็�นฟั�งก์�ชั�นที่��ใชั�ต่�ด้ชัองวางหน�า แล็ะ/หริ%อ ที่�ายข�อคืวาม่ โด้ย trim() ใชั�ต่�ด้ชัองวางหน�า แล็ะหล็�งข�อคืวาม่ ltrim() ใชั�ต่�ด้ชัองวางหน�าข�อคืวาม่ rtrim() ใชั�ต่�ด้ชัองวางที่�ายข�อคืวาม่ chop() ใชั�ต่�ด้ชัองวางที่�ายข�อคืวาม่ ริ-ป็แบบ trim(string string)

Page 41: PHP Tutorial (funtion)

41

ฟั�งก์�ชั�นต่างๆ ใน PHP ltrim(), rtrim(), trim(), chop()

Demo

<?php #trim.php

$email = " [email protected] ";

print "all ".".".$email.".<br>";

print "trim ".".".trim($email).".<br>";

print "ltrim ".".".ltrim($email).".<br>";

print "rtrim ".".".rtrim($email).".<br>";

print "chop ".".".chop($email).".<br>";

?>

Page 42: PHP Tutorial (funtion)

42

ฟั�งก์�ชั�นต่างๆ ใน PHP list()

เป็�นฟั�งก์�ชั�นที่��ใชั�ส,าหริ�บน,าคืาใสล็งในต่�วแป็ริ ริ-ป็แบบ list($var1[, $var2, [$var3, …]])

$vari ต่�วแป็ริต่�วที่�� i ที่��จะน,าคืาไป็ใส

Page 43: PHP Tutorial (funtion)

43

ฟั�งก์�ชั�นต่างๆ ใน PHP explode()

เป็�นฟั�งก์�ชั�นที่��ใชั�ส,าหริ�บแบงข�อคืวาม่ออก์เป็�นข�อคืวาม่ยอยๆ ริ-ป็แบบ array explode(string separator, string [, int limit]) separator ต่�วอ�ก์ษริหริ%อข�อคืวาม่ที่��ใชั�เป็�นต่�วแบงแยก์ข�อคืวาม่ string ข�อคืวาม่หล็�ก์ที่��ถ-ก์แบงแยก์เป็�นข�อคืวาม่ยอย limit ต่�วเล็ขริะบ2จ,านวนข�อคืวาม่ส-งส2ด้ที่��ให�สริ�างข�(น

Page 44: PHP Tutorial (funtion)

44

ฟั�งก์�ชั�นต่างๆ ใน PHP explode()

Demo

<?php #explode.php

$email = " [email protected] ";

list($name, $domain) = explode("@", $email);

print $name."<br>";

print $domain."<br>";

?>

Page 45: PHP Tutorial (funtion)

45

ฟั�งก์�ชั�นต่างๆ ใน PHP explode() ต่อ

Demo<?php #explode2.php

$email = " system@[email protected] ";list($sys, $name, $domain) = explode("@",$email,

3);print $sys."<br>";print $name."<br>";print $domain."<br>";

?>

Page 46: PHP Tutorial (funtion)

46

ฟั�งก์�ชั�นต่างๆ ใน PHP implode()

เป็�นฟั�งก์�ชั�นที่��ใชั�ในก์าริต่อคืาของแต่ล็ะ element ใน array หริ%อ ข�อคืวาม่เป็�นข�อคืวาม่เด้�ยว ริ-ป็แบบ string implode(string glue, array pieces) glue ต่�วอ�ก์ษริ คื,า หริ%อข�อคืวาม่ ที่��ใชั�เชั%�อม่ข�อคืวาม่ pieces คื%อ array ของคื,าหริ%อข�อคืวาม่

Page 47: PHP Tutorial (funtion)

47

ฟั�งก์�ชั�นต่างๆ ใน PHP implode()

Demo

<?php #implode.php

$email = "[email protected]";

list($name[], $domain) = explode("@", $email);

print $name[0]."<br>";

print $domain."<br>";

$name[] = "yahoo.com";

$newemail = implode("@", $name);

print $newemail."<br>";

?>

Page 48: PHP Tutorial (funtion)

48

ฟั�งก์�ชั�นต่างๆ ใน PHP implode()

Demo

<?php #implode2.php

<?php

$array = array('lastname', 'email', 'phone');

$comma_separated = implode("@", $array);

echo $comma_separated;

?>

Page 49: PHP Tutorial (funtion)

49

ฟั�งก์�ชั�นต่างๆ ใน PHP strtolower(), strtoupper()

เป็�นฟั�งก์�ชั�นที่��ใชั�ในก์าริแป็ล็งข�อคืวาม่เป็�นต่�วอ�ก์ษริพื่�ม่พื่�ใหญิหริ%อพื่�ม่พื่�เล็"ก์ โด้ย strtolower() ที่,าให�ข�อคืวาม่เป็�นต่�วพื่�ม่พื่�เล็"ก์ที่�(งหม่ด้ strtoupper() ที่,าให�ข�อคืวาม่เป็�นต่�วพื่�ม่พื่�ใหญิที่�(งหม่ด้

Demo

<?php #strtou.php

$email = "[email protected]"."<br>";

print strtoupper($email);

$email = "[email protected]";

print strtolower($email);

?>

Page 50: PHP Tutorial (funtion)

50

ฟั�งก์�ชั�นต่างๆ ใน PHP ucfirst(), ucwords()

เป็�นฟั�งก์�ชั�นที่��ใชั�ก์าริแป็ล็งข�อคืวาม่เป็�นต่�วอ�ก์ษริพื่�ม่พื่�ใหญิหริ%อพื่�ม่พื่�เล็"ก์ โด้ย ucfirst() ที่,าให�ต่�วอ�ก์ษริต่�วแริก์ของข�อคืวาม่เป็�นต่�วพื่�ม่พื่�ใหญิ ucwords() ที่,าให�ต่�วอ�ก์ษริต่�วแริก์ของ แต่ล็ะคื,า ในข�อคืวาม่เป็�นต่�วพื่�ม่พื่�ใหญิที่�(งหม่ด้

Demo

<?php #ucwords.php

$email = "chatchag@ tot.co.th hotmail.com"."<br>";

print ucwords($email);

$email = "chatchag@ tot.co.th hotmail.com";

print ucfirst($email);

?>

Page 51: PHP Tutorial (funtion)

51

ฟั�งก์�ชั�นต่างๆ ใน PHP strcmp()

เป็�นฟั�งก์�ชั�นที่��ใชั�ก์าริเป็ริ�ยบเที่�ยบ string 2 ชั2ด้ ริ-ป็แบบ int strcmp(string str1, string str2) str1, str2 คื%อสต่ริ�งสองคืาที่��น,าม่าเป็ริ�ยบเที่�ยบก์�น

Demo<?php #strcmp.php

$email1 = "[email protected]";$email2 = "[email protected]";print strcmp($email1, $email1)."<br>";print strcmp($email1, $email2)."<br>";print strcmp($email2, $email1)."<br>";

?>

Page 52: PHP Tutorial (funtion)

52

ฟั�งก์�ชั�นต่างๆ ใน PHP printf(), sprintf()

เป็�นฟั�งก์�ชั�นที่��ใชั�ก์าริจ�ด้ริ-ป็แบบข�อคืวาม่ให�เป็�นไป็ต่าม่ริ-ป็แบบที่��ต่�องก์าริริ-ป็แบบ

void printf(string format [,mixed args])

string sprintf(string format [,mixed args])

ว�ธี�ก์าริสริ�างริ-ป็แบบข�อคืวาม่จะที่,าได้�โด้ยก์าริสริ�างข�อคืวาม่ที่��ม่� ต่�ว“ริะบ2ชัน�ด้ หริ%อ ” type specifier แที่ริก์อย-ในข�อคืวาม่ โด้ยจะต่�องน,าต่�วริะบ2ชัน�ด้ด้�วยเคืริ%�องหม่าย %

Page 53: PHP Tutorial (funtion)

53

ฟั�งก์�ชั�นต่างๆ ใน PHP printf(), sprintf()

ต่าริางแสด้งต่�วริะบ2ชัน�ด้ (type specifier) b ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�นเล็ขฐานสอง c ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�น Ascii code d ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�นเล็ขฐานส�บ ม่� เคืริ%�องหม่าย u ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�นเล็ขฐานส�บ ไม่ม่�เคืริ%�องหม่าย f ที่,าให�คืา argument เป็�นเล็ขที่ศน�ยม่ o ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�นเล็ขฐานแป็ด้ s ที่,าให�คืา argument เป็�น string x ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�นเล็ขฐานส�บหก์ ด้�วยอ�ก์ษริ พื่�ม่พื่�เล็"ก์ b ที่,าให�คืา argument เป็�นเล็ขจ,านวนเต่"ม่ แล็ะแสด้งเป็�นเล็ขฐานส�บหก์ ด้�วยต่�วอ�ก์ษริพื่�ม่พื่�ใหญิ

Page 54: PHP Tutorial (funtion)

54

ฟั�งก์�ชั�นต่างๆ ใน PHPDemo

<? //sprintf.php

$format = "chocolate 2 %s, is 129 %s. ";

$output = sprintf($format, "scoop(s)", "Baht");

print $output."<br>";

?>

<? //printf.php

$model = "AF-111";$unitprice = "25230.255";

$format = “ส�นคื�า %s ริาคืาต่อหนวย = %.2f บาที่";

printf($format, $model, $unitprice)."<br>";

?>

Page 55: PHP Tutorial (funtion)

55

ฟั�งก์�ชั�นต่างๆ ใน PHP is_int(), is_integer()

ใชั�ในก์าริต่ริวจสอบด้-วาคืาพื่าริาม่�เต่อริ�ที่��ริ �บม่าเป็�น integer หริ%อไม่Demo

<? //isint.php

$i = 1.30;

#settype($i, "integer");

if(is_int($i))

print $i." is an integer.<br>";

else

print $i." is not an integer.<br>";

?>

Page 56: PHP Tutorial (funtion)

56

ฟั�งก์�ชั�นต่างๆ ใน PHP is_float(), is_double()

ใชั�ในก์าริต่ริวจสอบด้-วาคืาพื่าริาม่�เต่อริ�ที่��ริ �บม่าเป็�นต่�วเล็ขที่ศน�ยม่หริ%อไม่

Demo

<? //isfloat.php

$i = 1.30;

#settype($i, "integer");

if(is_float($i))

print $i." is an float.<br>";

else

print $i." is not an float.<br>";

?>

Page 57: PHP Tutorial (funtion)

57

ฟั�งก์�ชั�นต่างๆ ใน PHP decbin(), bindec()

decbin() ใชั�ในก์าริแป็ล็งเล็ขฐานส�บเป็�นฐานสอง bindec() ใชั�ในก์าริแป็ล็งฐานสองเป็�นเล็ขฐานส�บ

Demo <? //decbin.php

$d = 10;print $d." is ".decbin($d).".<br>";$b = 1001;print $b." is ".bindec($b).".<br>";

?>

Page 58: PHP Tutorial (funtion)

58

ฟั�งก์�ชั�นต่างๆ ใน PHP decoct(), octdec()

decoct() ใชั�ในก์าริแป็ล็งเล็ขฐานส�บเป็�นฐานแป็ด้ octdec() ใชั�ในก์าริแป็ล็งฐานแป็ด้เป็�นเล็ขฐานส�บ

Demo <? //decoct.php

$d = 10;print $d." is ".decoct($d).".<br>";$o = 20;print $o." is ".octdec($o).".<br>";

?>

Page 59: PHP Tutorial (funtion)

59

ฟั�งก์�ชั�นต่างๆ ใน PHP dechex(), hexdec()

dechex() ใชั�ในก์าริแป็ล็งเล็ขฐานส�บเป็�นฐานส�บหก์ hexdec() ใชั�ในก์าริแป็ล็งฐานส�บหก์เป็�นเล็ขฐานส�บ

Demo <? //dechex.php

$d = 10;print $d." is ".dechex($d).".<br>";$h = a;print $h." is ".hexdec($h).".<br>";

?>

Page 60: PHP Tutorial (funtion)

60

ฟั�งก์�ชั�นต่างๆ ใน PHP floor(), ceil(), round()

ใชั�ในก์าริป็�ด้เศษที่ศน�ยม่ ด้�วยเง%�อนไขที่��แต่ก์ต่างก์�น คื%อ floor() ป็�ด้เศษที่�(ง ไม่น,าม่าคื�ด้ ที่2ก์ก์ริณี� ceil() ป็�ด้เศษข�(น round() ป็�ด้เศษ ต่าม่คืวาม่เหม่าะสม่ ริ-ป็แบบ float round(float val [, int precision]) val คื%อคืาจ,านวนต่�วเล็ขหล็�งจ2ด้ที่ศน�ยม่ที่��ต่�องก์าริ สาม่าริถริะบ2เป็�น 0 บวก์ หริ%อ คืาล็บก์"ได้� default เป็�น 0 คืาที่��เป็�นบวก์จะที่,าให�เล็ขหล็�งจ2ด้ที่ศน�ยม่ถ-ก์ป็�ด้เศษ คืาที่��เป็�นล็บจะที่,าให�เล็ขหน�าจ2ด้ที่ศน�ยม่ถ-ก์ป็�ด้เศษ

Page 61: PHP Tutorial (funtion)

61

ฟั�งก์�ชั�นต่างๆ ใน PHP floor(), ceil(), round()

Demo <?php #round.php

$num1 = 123.2563;$num2 = 235.2566;$avg = ($num1 + $num2)/2;print $avg."<br>";print round($avg,2)."<br>";print round($avg,-1)."<br>";print floor($avg)."<br>";print ceil($avg)."<br>";

?>