ซื้อขายบ้านมือสอง

วันอังคารที่ 9 ตุลาคม พ.ศ. 2550

รู้จักกับ ตัวแปรประเภทต่างๆ ของ PHP





ตัวแปรประเภทต่างๆ ของ PHP


ในการเขียนโปรแกรม เราจำเป็นต้องรู้จักกับ ตัวแปร ซึ่งตัวแปรก็คือที่เก็บพักข้อมูล หรือเก็บค่าต่างๆ ไว้ใช้ในโปรแกรม


ตัวแปร (Variables)


ตัวแปร มีหน้าที่เก็ฐค่าตัวเลข ตัวอักษร หรือชุดข้อมูล เพื่อใช้ในการอ้างอิง โดยในการตั้งชื่อตัวแปร ใน php นั้นมีกฏดังนี้


1. ตัวแปรจะต้องขึ้นต้นด้วย $ (Dollar Sign) จากนั้นต้องตามด้วยตัวอักษร ห้ามขึ้นต้นด้วยตัวเลขหรือสัญลักษณ์


2. ต้องไม่มีสัญลักษณ์ทางคณิตศาสตร์มาเกี่ยวข้อง


3. ชื่อตัวแปรห้ามเว้นวรรค


4. ตัวอักษรตัวเล็กตัวใหญ่ สำคัญมาก เป็นลักษณะ Case Sensitive


$var1=10;


$var2=”20”;


echo $var1.”<br>”;


echo $var2.”<br>”;


การกำหนดค่าให้กับตัวแปร เราสามารถทำได้โดยการ ใช้เครื่องหมายเท่ากับ = ตามด้วยค่าที่จะกำหนด หากค่าเป็น ข้อความให้ใส่ เครื่องหมายคำพูด “…” หรือ ‘….’


Echo คือคำสั่งที่ใช้แสดง ข้อความในหน้า เว็บเพจ หรืออาจจะใช้คำสั่ง print ก็ได้ ลักษณะการเขียน เราเขียนได้ ดังนี้


Echo “ข้อความที่จะแสดง”;


ข้อความที่จะให้แสดง จะต้องอยู่ภายในเครื่องหมาย “..” ซึ่งเราอาจจะ ใช้เป็น ‘ ก็ได้












ตัวแปร Scope


ตัวแปร Scope เป็นระดับการเข้าถึงของตัวแปร ยกตัวอย่าง โคดดังต่อไปนี้


Example : var_scope.php


<?php


$a=10; // ประกาศตัวแปร a มีค่าเท่ากับ 10


function testscope(){
$a=20;


echo $a.”<br>”; // แสดงข้อมูล ตัวแปร a ออกทางหน้าจอ


}


testscope(); // เรียกใช้ ฟังชั่น testscope


echo $a;


?>



จากโคดที่เขียนขึ้นมา เมื่อมีการเรียกใช้ตัวแปร a ซึ่งถูกประกาศไว้ นอกฟังชั่น ภายในฟังชั่น ก็มีการประกาศตัวแปร a อีกเช่นกัน ซึ่ง ใน php จะมองว่า ตัวแปร a ที่อยู่นอกฟังชั่น และ ตัวแปร a ที่อยู่ภายในฟังชั่นนั้น เป็นคนละตัวกัน ตัวแปร a ที่ประกาศ ภายในฟังชั่นนั้น เมื่อฟังชั่นทำงานเรียบร้อยแล้ว ตัวแปร a ภายในฟังชั่นจะถูกทำลายลง


หากต้องการให้ โปรแกรมสามารถเรียกใช้ตัวแปร a ที่อยู่ภายนอก เป็นตัวแปรตัวเดียวกัน กับที่อยู่ภายในฟังชั่นนั้น เราสามารถทำได้ ด้วยการใช้ ตัวแปร global


Example: var_global.php
<?php


$a=10; // ประกาศตัวแปร a มีค่าเท่ากับ 10


$b=20; // ประกาศตัวแปร b มีค่าเท่ากับ 20


function sum(){


global $a,$b;


$b= $a+$b; // เปลี่ยนค่า ตัวแปร b


}


sum(); // เรียกใช้ ฟังชั่น sum ทำให้ค่าตัวแปร b เปลี่ยนไป


echo $b;


?>







ตัวแปรชนิด Superglobal


ตัวแปรชนิด นี้เป็นตัวแปรซึ่งเราสามารถเรียกใช้ได้เลยที่ไหนก็ได้ใน โปรแกรม


ซึ่งตัวแปรแบบ superglobal นี้จะเป็นตัวแปรชนิด array เราจำเป็นจะต้อง ระบุkeyword เข้าไปในตัวแปรชนิดนี้ด้วย ตัวแปรนี้มีอยู่ด้วยกันหลายตัวเช่น


$_SERVER
ตัวแปร SERVER นี้เราจะใช้ Keyword เป็น ตัวแปร Environment ซึ่งเราสามารถ รู้ได้จาก phpinfo ยกตัวอย่าง การใช้ตัวแปร SERVER






Example : ip.php


Your computer IP is :


<?php


echo $_SERVER['REMOTE_ADDR'];


?>




$_GET
ตัวแปร superglobal GET นี้เราจะใช้สำหรับ รับค่าที่ส่งมาในรูปแบบ Get คือค่าที่ถูกส่งต่อท้าย url ยกตัวอย่างการใช้ตัวแปร get
http://localhost/var_get.php?myname=nuijang&myage=26






Example : var_get.php


<?php


$name=$_GET[“myname”];


$age=$_GET[“myage”];


echo “<b>ชื่อของคุณคือ :</b> “.$name.”<br>”;


echo “<b>อายุ :</b> ”.$age;


?>


$_POST
ตัวแปร superglobal POST นี้เราจะใช้สำหรับ รับค่าที่ส่งมาในรูปแบบ post หรือค่าที่ถูกส่งมาจากฟอร์ม


$_COOKIE
เป็นการเรียกใช้ ตัวแปรจาก cookie


$_FILES
เป็นการเรียกใช้ตัวแปรฝ่าย HTTP POST FILE การ upload file


$_ENV
เรียกใช้ ตัวแปร Environment


$_REQUEST
ตัวแปร Superglobal แบบ REQUEST นี้จะเป็นการรวมเอารูปแบบ แบบ GET , POST,COOKIE ไว้ที่ตัวแปร REQUEST เราสามารถเรียกใช้ตัวแปร REQUEST แทนการรับค่า ได้ทั้งแบบ GET , POST และ COOKIE


$_GLOBALS
เป็นตัวแปรที่ใช้สำหรับอ้างถึง ตัวแปร แบบ global ที่เรียกใช้งานผ่าน scope







ชนิดของข้อมูล (Data Type)


เนื่องจากข้อมูลที่เก็บอยู่ในตัวแปร มีรูปแบบที่ต่างกันออกไป ทำให้ชนิดของข้อมูล แต่ละชนิด มีขนาดของการเก็บข้อมูลในหน่วยความจำของคอมพิวเตอร์ไม่เท่ากัน และในการทำงานของโปรแกรมเราก็เช่นกัน หากกำหนด ชนิดข้อมูลผิดชนิด โปรแกรมอาจจะทำงานผิดพลาดไปเลยก็ได้ เราจำเป็นต้องกำหนดชนิดของข้อมูลให้เหมาะสม ซึ่งชนิดข้อมูลใน PHP สามารถแบ่งออกได้ดังนี้


1. integer ชนิดข้อมูลประเภทตัวเลขจำนวนเต็ม


2. floating point numbers ชนิดข้อมูลประเภทเลขทศนิยม


3. string ชนิดข้อมูลแบบอักขระ หรือ ข้อความ


4. Boolean ชนิดข้อมูลแบบนี้จะมีเพียง true กับ false


5. array ชุดข้อมูล ซึ่ง array ของ php สามารถเก็บข้อมูลได้หลายชนิด


6. object ตัวแปร object


Integer ตัวแปรชนิดตัวเลข ตัวแปลชนิดนี้จะเก็บ เลขจำนวนเต็ม ตัวอย่างเช่น


Example : var_integer.php


<?php


$a=100;


$b=150;


$c=-20;


$sum=$a+$b+$c;


echo '$a = '.$a.'<br>';


echo '$b = '.$b.'<br>';


echo '$c = '.$c.'<br>';


echo '$a+$b+$c = '.$sum;


?>



floating point numbers ตัวแปรชนิด ทศนิยม


Example : var_float.php


<?php


$num1 = 3.14;


$num2 = 20;


$num3 = (int) $num1 + $num2 ;


echo "\$num1 = $num1<br>";


echo "\$num2 = $num2<br>";


echo "\$num3 = $num3<br>";


?>


ตัวแปรชนิด string เราจะใช้เครื่องหมาย “ คร่อมไว้หัวท้ายของชุดอักษร เพื่อบอกว่าเป็นข้อมูลชนิด string หรือเราอาจจะใช้ ‘ ในการบอกว่าเป็นชนิดของ string ก็ได้ หรือหากเราต้องการแสดง อักขระบางตัวใน String เราสามารถใช้ \ ในการแสดงอักขระเหล่านั้นได้เช่น

























\\


แสดงอักขระ \ ใน string


\$


แสดง $ ใน string


\”


แสดงตัว “


\’


แสดงตัว ‘


\n


ขั้นบรรทัดใหม่


\r


ตัวยก


\t


horizontal tab








หากเราต้องการจะต่อ string สามารถทำได้ด้วยการใช้ตัวดำเนินการคือ . ในการเชื่อมต่อ


Example : var_string.php
<?php


$firstname="sarawut";


$lastname="chongcharoenmunkhong";


echo "\$firstname is :" . $firstname . "<br>";


echo "\$lastname is :" . $lastname . "<br>";


echo $firstname." ".$lastname;


?>


ฟังชั่นที่ใช้เกี่ยวกับ String ที่ใช้บ่อยๆ


Substr(string,start,end); เป็นฟังชั่นที่ใช้ตัดตัวอักษรที่ต้องการใช้ออกมา


String คือ ชุดข้อความทั้งหมดที่ต้องการนำมาตัด


Start คือ เป็นข้อมูลชนิดตัวเลข หมายถึงตำแหน่งเริ่มตัด (หากค่าตัวเลขเป็นติดลบ หมายถึง นับตำแหน่งจากหลังสุดมาข้างหน้า


End คือ เป็นข้อมูลชนิดตัวเลข หมายถึงจำนวนตัวอักษรที่ต้องการตัด ซึ่งจะระบุหรือไม่ก็ได้ หากไม่ระบุหมายถึงให้ตัดออกมาตั้งแต่ตำแหน่ง start ไปจนจบข้อความ string



Example : func_string_substr.php


<?php


$string = "0123456789";


echo substr($string,0)."<br>"; // print 0123456789


echo substr($string,0,5)."<br>"; // print 01234


echo substr($string,-2,1)."<br>"; // print 8


echo substr($string,3,4)."<br>"; // print 3456


?>



strlen(string); ฟังชั่นที่ใช้ในการนับจำนวนความยาวของข้อความ


string คือ ชุดข้อมูลทั้งหมดที่ต้องการนับ


Example : func_string_strlen.php


<?php


$string = "0123456789";


echo strlen($string); // print 10


?>



trim(string); เป็นฟังชั่นที่ใช้สำหรับตัดข้อความ ที่เป็นช่องว่าง ด้านหน้า และด้านหลังออก


strrev(string); เรียงสลับข้อความจากหลังมาหน้า


eregi_replace(คำค้นหา,คำแทนที่,ข้อมูล); แทนที่ข้อความที่พบ


htmlspecialchars(string); แทรกแทก html ลงในเว็บเพจ


addslashes(string) แทรก \ ไว้สำหรับข้อความที่มีอักขระ โดยอัตโนมัติ



ตัวแปรชนิด Array เป็นตัวแปรที่เก็บข้อมูลเป็นชุด ซึ่ง ตัวแปร array นี้ใน php สามารถเก็บข้อมูลได้ หลายชนิดด้วยกัน โดยที่การเก็บข้อมูลเป็นชุดนี้จะมีลำดับของสมาชิก เราเรียกว่า element ซึ่งแต่ละ element จะมี index เป็นตัวชี้ตำแหน่งของข้อมูล index จะเริ่มจาก 0 และเพิ่มไปเรื่อยๆ การสร้างตัวแปร Array สามารถทำได้หลายวิธี ดังต่อไปนี้


วิธีที่ 1 สร้างผ่านฟังชั่น Array()


Example : var_array01.php


<?php


$myarr = array("sarawut", "chongcharoenmunkhong", "PHP");



for ($i=0; $i<=count($myarr)-1; $i++) {


echo $myarr[$i] . "<br>\n";


}


?>


วิธีที่ 2 การสร้างโดย ใช้ Array indentifier


Example : var_array02.php


<?php


$myarr2[] = "nui";


$myarr2[] = "beer";


$myarr2[] = "num";


$myarr2[] = "aey";


$myarr2[] = "ooy";



echo $myarr2[2];


?>


การสร้างตัวแปร Array แบบ 2 มิติ


<?php


$myarr2D = array("notebook",


array("ibm","acer","hp","benq"));



echo $myarr2D[0]."<br>";


for ($i=0;$i<count($myarr2D[1]);$i++){


echo $myarr2D[1][$i]."<br>";


}


?>



ฟังชั่นที่ใช้กับ Array ฟังชั่นที่ใช้บ่อยๆ ใน Array คือ


Sort(ตัวแปร Array) = คือการเรียงลำดับ ข้อมูลใน array จาก น้อยไปหามาก


Rsort(ตัวแปร array) = คือการเรียงลำดับ ข้อมูลใน array จากมากไปหาน้อย










ตัวแปรชนิด Object


ในการใช้งาน Class และ Object นั้นผู้ใช้ควรมีควมเข้าใจในหลักการของ Object Oriented Programming ก่อนพอสมควร


การพัฒนาโปรแกรมแบบ Object ผู้เขียนโปรแกรมจะต้องเขียนโครงสร้างและ Fuction ต่างของ Object ก่อนจากนั้น จึงทำการสร้างตัวแปรให้เป็นข้อมูลประเภทของ Object นั้นๆ ดังตัวอย่างต่อไปนี้


Example : var_object_class.php
<?php
class myclass {
// property
var $name;
var $age;
var $sex;
// method ที่ใช้สำหรับแสดงข้อมูลสมาชิก
function getMember() {
echo "You name :".$this->name."<br>";
echo "You age :".$this->age."<br>";
echo "You sex :".$this->sex."<br>";
}
}


// สร้างตัวแปร Object ขั้นมาจาก class myclass 2 class
$myMem1 = new myclass;
$myMem2 = new myclass;
// กำหนดค่าให้กับ object myMem1
$myMem1->name="nuijang";
$myMem1->age=26;
$myMem1->sex="male";
// กำหนดค่าให้กับ object myMem2
$myMem2->name="nus_kung";
$myMem2->age=35;
$myMem2->sex="female";
// ==========================================
$myMem1->getMember(); // เรียกใช้ method ในการแสดงข้อมูล
$myMem2->getMember(); // เรียกใช้ method ในการแสดงข้อมูล
?>


9 ความคิดเห็น:

Unknown กล่าวว่า...

Rolex was aswell the aboriginal Cool Watches aggregation to actualize a absolutely waterproof watch - accession anniversary from change to anatomic timepiece. Wilsdorf even went so far as to accept a distinctively bogus Rolex watch absorbed to the ancillary of the Trieste bathyscaphe, which went to the basal of the Mariana Trench.

ไม่ระบุชื่อ กล่าวว่า...

I ωaѕ suggestеԁ thiѕ blog by my сouѕin.
I аm not sure whether thіs post is written by him
as no one elsе know such detаiled about my tгoublе.
Υou're amazing! Thanks!

my blog - dallas auto insurance
My website > auto insurance dallas tx

ไม่ระบุชื่อ กล่าวว่า...

Hey there! I could havе sworn I've been to this site before but after reading through some of the post I realized it's new to me.
Nonеthelеss, ӏ'm definitely happy I found it and I'll be
booκ-marking and checking back frequently!


my web-site; dallas tx car insurance

ไม่ระบุชื่อ กล่าวว่า...

Hello just wantеd to give you a quісk headѕ up.
Thе wоrԁs in youг contеnt sеem tο
be running off the sсгeеn in Safarі.
I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I figured I'd ροst
to let you knoω. The design loοk great though!
Hoрe уou get the issue fixed soon.
Κudos

Feel frеe to suгf to my web blog: buy a bucket truck

ไม่ระบุชื่อ กล่าวว่า...

Hello! I could have swοrn I've visited this blog before but after looking at some of the articles I realized it's nеw to me.

Rеgardless, ӏ'm certainly delighted I stumbled upon it and I'll be bоok-markіng it anԁ checking back frequentlу!


Also visіt my ωеbsite :: taxicab coppell

ไม่ระบุชื่อ กล่าวว่า...

buy valium valium recreational usage - valium pills dose

ไม่ระบุชื่อ กล่าวว่า...

Appreсiate the reсommendation. Wіll try it out.


my webpage - how to flip cars at an auction

ไม่ระบุชื่อ กล่าวว่า...

Нello my loνed one! I wаnt to say thаt
this post is awesome, nice wrіtten and incluԁe almost all important іnfos.
I would like tо lοoκ more posts lіkе this .


Loοk аt my web ѕite :
: seo company dallas tx

ไม่ระบุชื่อ กล่าวว่า...

cheap tramadol no prescription buy tramadol online no prescription - buy tramadol india