SlideShare a Scribd company logo
1 of 10
Download to read offline
สวัสดีครับวันนี้จะนำเสนอบทเรียน
เรื่อง ตัวแปร ( variables ) ซึ่ง
เป็นองค์ประกอบในกำรเขียน
โปรแกรมด้วยภำษำ PHP เรำมำ
เรียนรู้กันเลยนะครับ
หน่วยการเรียนรู้ที่ 3
องค์ประกอบภาษา PHP
ตัวแปร (variables)
ตัวแปรคืออะไร ตัวแปรมีหน้ำที่เก็บค่ำ
ตัวเลข ตัวอักษร หรือชุดข้อมูล เพื่อใช้
ในกำรอ้ำงอิง
กำรตั้งชื่อตัวแปรใน php มีกฎ
ดังนี้
1. ตัวแปรจะต้องขึ้นต้นด้วย $
(Dollar Sign) จำกนั้นต้องตำมด้วย
ตัวอักษร ห้ำมขึ้นต้นด้วยตัวเลขหรือ
สัญลักษณ์
2. ต้องไม่มีสัญลักษณ์
ทำงคณิตศำสตร์มำ
เกี่ยวข้อง
3. ชื่อตัวแปรห้ำมเว้นวรรค
4. กำรตั้งชื่อตัวแปรสำคัญมำกนะ เรำสำมำรถตั้งชื่อได้ทั้งภำษำไทย
ภำษำอังกฤษ แต่ส่วนมำกนิยมภำษำอังกฤษ และกำรใช้ตัวอักษร
ในภำษำอังกฤษอักษรพิมพ์เล็กพิมพ์ใหญ่ก็สำคัญ ใช้ให้ถูกเพรำะ
เป็นลักษณะ Case sensitive เพรำะถ้ำพิมพ์ผิดจะกลำยเป็น
คนละตัวแปรกัน ไม่ว่ำกำรใช้ ตัวเลข( 0-9) สัญลักษณ์ _
(underscore) ต้องใช้ให้ถูก เช่น $a จะถูกมองว่ำเป็นคน
ละตัวแปรกับ $A
อยำกทรำบ
ว่ำตัวแปรมี
กี่แบบ กี่
ชนิดค่ะ
ตัวแปรมีทั้งหมด 4 แบบดังนี้
1. Integer สำหรับจัดเก็บข้อมูลที่เป็น
ตัวเลขจำนวนเต็ม เช่น 1000, -1000
2. Float สำหรับจัดเก็บข้อมูลที่เป็น
ทศนิยม เช่น 123.231
3. Strings สำหรับจัดเก็บข้อมูลที่เป็น
ข้อควำม
เช่น Thamamakwitthayakom",
"kanchanaburi" "Thailand"
4. Arrays สำหรับจัดเก็บข้อมูลที่เป็น
ข้อมูลที่เป็นชุด
เรำมำดูตัวอย่ำงตัวแปรแต่ละชนิดกันนะ
ตัวอย่ำงที่ 1 Integer สำหรับจัดเก็บข้อมูลที่เป็นตัวเลขจำนวน
เต็ม เช่น 1000, -1000
<?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;
?>
ตัวอย่ำงที่2 Float สำหรับจัดเก็บข้อมูลที่เป็น
ทศนิยม เช่น 123.231
<?php
$num1 = 3.14;
$num2 = 20;
$num3 = (int) $num1 + $num2 ;
echo "$num1 = $num1<br>";
echo "$num2 = $num2<br>";
echo "$num3 = $num3<br>";
?>
ตัวอย่ำงที่ 3 Strings สำหรับจัดเก็บข้อมูลที่เป็นข้อควำม
<?php
$firstname="sakorn";
$lastname="Nontarak";
echo "$firstname is :" . $firstname . "<br>";
echo "$lastname is :" . $lastname . "<br>";
echo $firstname." “ .$lastname;
?>
ตัวอย่ำงที่ 4 Arrays สำหรับจัดเก็บข้อมูลที่
เป็นข้อมูลที่เป็นชุด
<?php
$myarr = array("sakorn", "Nontarak", "PHP");
for ($i=0; $i<=count($myarr)-1; $i++) { }
?>
นอกจำกนี้ ตัวแปร Arrays ยังสร้ำงได้แบบ 2 มิติ
<?php
$myarr2D = array("notebook",
array("ibm","acer","hp","benq"));
echo $myarr2D[0]."<br>";
for
($i=0;$i<COUNT($MYARR2D[1]);$
I++)
{
$myarr2D[1][$i].?<br echo>";
}
?>
ตัวแปร Array แบบ 2 มิติ
จบกำรนำเสนอเรื่องตัวแปรใน
ภำษำ PHP

More Related Content

What's hot

ตัวแปรในภาษาซี
ตัวแปรในภาษาซีตัวแปรในภาษาซี
ตัวแปรในภาษาซี
boky_peaw
 
การตั้งชื่อตัวแปร
การตั้งชื่อตัวแปรการตั้งชื่อตัวแปร
การตั้งชื่อตัวแปร
mycomc55
 
1. ประวัติภาษาซี
1. ประวัติภาษาซี1. ประวัติภาษาซี
1. ประวัติภาษาซี
mansuang1978
 
ภาษา+Fort...pdfกรกช
ภาษา+Fort...pdfกรกชภาษา+Fort...pdfกรกช
ภาษา+Fort...pdfกรกช
Korakot Kaevwichian
 
การเขียนคำสั่งควบคุม
การเขียนคำสั่งควบคุมการเขียนคำสั่งควบคุม
การเขียนคำสั่งควบคุม
Kashima Seto
 

What's hot (17)

การเขียนคำสั่งข้อมูลขั้นพื้นฐาน
การเขียนคำสั่งข้อมูลขั้นพื้นฐานการเขียนคำสั่งข้อมูลขั้นพื้นฐาน
การเขียนคำสั่งข้อมูลขั้นพื้นฐาน
 
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
 
ตัวแปรในภาษาซี
ตัวแปรในภาษาซีตัวแปรในภาษาซี
ตัวแปรในภาษาซี
 
การตั้งชื่อตัวแปร
การตั้งชื่อตัวแปรการตั้งชื่อตัวแปร
การตั้งชื่อตัวแปร
 
บทที่ 2 ตัวแปร
บทที่ 2 ตัวแปรบทที่ 2 ตัวแปร
บทที่ 2 ตัวแปร
 
ตัวแปรกับชนิดของข้อมูล
ตัวแปรกับชนิดของข้อมูลตัวแปรกับชนิดของข้อมูล
ตัวแปรกับชนิดของข้อมูล
 
ภาษา C เบื้องต้น
ภาษา C เบื้องต้นภาษา C เบื้องต้น
ภาษา C เบื้องต้น
 
การเขียนคำสั่งควบคุม
การเขียนคำสั่งควบคุมการเขียนคำสั่งควบคุม
การเขียนคำสั่งควบคุม
 
การเขียนโปรแกรมภาษาซี
การเขียนโปรแกรมภาษาซีการเขียนโปรแกรมภาษาซี
การเขียนโปรแกรมภาษาซี
 
1. ประวัติภาษาซี
1. ประวัติภาษาซี1. ประวัติภาษาซี
1. ประวัติภาษาซี
 
ภาษา+Fort...pdfกรกช
ภาษา+Fort...pdfกรกชภาษา+Fort...pdfกรกช
ภาษา+Fort...pdfกรกช
 
งาน
งานงาน
งาน
 
การเขียนคำสั่งควบคุม
การเขียนคำสั่งควบคุมการเขียนคำสั่งควบคุม
การเขียนคำสั่งควบคุม
 
ตัวแปรพื้นฐานเขียนโปรแกรม
ตัวแปรพื้นฐานเขียนโปรแกรมตัวแปรพื้นฐานเขียนโปรแกรม
ตัวแปรพื้นฐานเขียนโปรแกรม
 
Lab Computer Programming 1
Lab Computer Programming 1Lab Computer Programming 1
Lab Computer Programming 1
 
ชื่อ (Identifier)
ชื่อ (Identifier)	ชื่อ (Identifier)
ชื่อ (Identifier)
 
หน่วยที่1 ชื่อ (Identifier)
หน่วยที่1 ชื่อ (Identifier)	หน่วยที่1 ชื่อ (Identifier)
หน่วยที่1 ชื่อ (Identifier)
 

Viewers also liked

ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
bpatra
 
Technical+presentation+4+seminar
Technical+presentation+4+seminarTechnical+presentation+4+seminar
Technical+presentation+4+seminar
amitbabbar1
 
On line parent information session
On line parent information sessionOn line parent information session
On line parent information session
qldacademies
 
PRINT A5-SPIRALHIRES
PRINT A5-SPIRALHIRESPRINT A5-SPIRALHIRES
PRINT A5-SPIRALHIRES
Dita Koswara
 
On line parent information session
On line parent information sessionOn line parent information session
On line parent information session
qldacademies
 

Viewers also liked (20)

Unit1 ph pprogram
Unit1 ph pprogramUnit1 ph pprogram
Unit1 ph pprogram
 
ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
 
Answer unit2.1
Answer unit2.1Answer unit2.1
Answer unit2.1
 
การเขียนโปรแกรมเบื้องต้น
การเขียนโปรแกรมเบื้องต้นการเขียนโปรแกรมเบื้องต้น
การเขียนโปรแกรมเบื้องต้น
 
Answer unit1.2
Answer unit1.2Answer unit1.2
Answer unit1.2
 
Technical+presentation+4+seminar
Technical+presentation+4+seminarTechnical+presentation+4+seminar
Technical+presentation+4+seminar
 
Answer unit4.2.5
Answer unit4.2.5Answer unit4.2.5
Answer unit4.2.5
 
Clever Creative Global - Queensland Academies Creative Industries Campus
Clever Creative Global - Queensland Academies Creative Industries CampusClever Creative Global - Queensland Academies Creative Industries Campus
Clever Creative Global - Queensland Academies Creative Industries Campus
 
Unit2flowchart
Unit2flowchartUnit2flowchart
Unit2flowchart
 
On line parent information session
On line parent information sessionOn line parent information session
On line parent information session
 
Business Continuity for the Small to Medium Business
Business Continuity for the Small to Medium BusinessBusiness Continuity for the Small to Medium Business
Business Continuity for the Small to Medium Business
 
PRINT A5-SPIRALHIRES
PRINT A5-SPIRALHIRESPRINT A5-SPIRALHIRES
PRINT A5-SPIRALHIRES
 
On line parent information session
On line parent information sessionOn line parent information session
On line parent information session
 
What QAHS staff are reading
What QAHS staff are readingWhat QAHS staff are reading
What QAHS staff are reading
 
Answer unit4.1.2
Answer unit4.1.2Answer unit4.1.2
Answer unit4.1.2
 
Intro program php
Intro program phpIntro program php
Intro program php
 
Unit4maintain
Unit4maintainUnit4maintain
Unit4maintain
 
Unit3coding
Unit3codingUnit3coding
Unit3coding
 
Programstructure
ProgramstructureProgramstructure
Programstructure
 
Unit4maintain edit
Unit4maintain editUnit4maintain edit
Unit4maintain edit
 

Similar to Unit3.1variables

Pbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้นPbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้น
Dararat Worasut
 
การพัฒนาโปรแกรม วิชญา เลขที่ 26.2
การพัฒนาโปรแกรม วิชญา เลขที่ 26.2การพัฒนาโปรแกรม วิชญา เลขที่ 26.2
การพัฒนาโปรแกรม วิชญา เลขที่ 26.2
winewic199
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
Thank Chiro
 

Similar to Unit3.1variables (20)

Presentation1
Presentation1Presentation1
Presentation1
 
Work
WorkWork
Work
 
3.2 ตัวแปรและคำสงวน
3.2 ตัวแปรและคำสงวน3.2 ตัวแปรและคำสงวน
3.2 ตัวแปรและคำสงวน
 
Pbl2 docx
Pbl2 docxPbl2 docx
Pbl2 docx
 
Pbl2 docx
Pbl2 docxPbl2 docx
Pbl2 docx
 
Pbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้นPbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้น
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
รายงาน PHP - Know2pro.com
รายงาน PHP - Know2pro.comรายงาน PHP - Know2pro.com
รายงาน PHP - Know2pro.com
 
บทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Javaบทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Java
 
บทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Javaบทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Java
 
โครงสร้างของภาษา C
โครงสร้างของภาษา Cโครงสร้างของภาษา C
โครงสร้างของภาษา C
 
ไฟล์ Presentation ประกอบรายงาน PHP - Know2Pro.co.cc
ไฟล์ Presentation ประกอบรายงาน PHP - Know2Pro.co.ccไฟล์ Presentation ประกอบรายงาน PHP - Know2Pro.co.cc
ไฟล์ Presentation ประกอบรายงาน PHP - Know2Pro.co.cc
 
การพัฒนาโปรแกรม วิชญา เลขที่ 26.2
การพัฒนาโปรแกรม วิชญา เลขที่ 26.2การพัฒนาโปรแกรม วิชญา เลขที่ 26.2
การพัฒนาโปรแกรม วิชญา เลขที่ 26.2
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
ตัวแปรกับชนิดของข้อมูล1
ตัวแปรกับชนิดของข้อมูล1ตัวแปรกับชนิดของข้อมูล1
ตัวแปรกับชนิดของข้อมูล1
 
ตัวแปร การกำหนดค่าและชนิดข้อมูล
ตัวแปร การกำหนดค่าและชนิดข้อมูลตัวแปร การกำหนดค่าและชนิดข้อมูล
ตัวแปร การกำหนดค่าและชนิดข้อมูล
 
การเขียนเว็บไซต์ด้วยภาษาPhp
การเขียนเว็บไซต์ด้วยภาษาPhpการเขียนเว็บไซต์ด้วยภาษาPhp
การเขียนเว็บไซต์ด้วยภาษาPhp
 
3.2 ตัวแปรและคำสงวน
3.2 ตัวแปรและคำสงวน3.2 ตัวแปรและคำสงวน
3.2 ตัวแปรและคำสงวน
 
Pbl2
Pbl2Pbl2
Pbl2
 
Basic
BasicBasic
Basic
 

More from KwanJai Cherubstar (16)

Introprogramphp
IntroprogramphpIntroprogramphp
Introprogramphp
 
ความรู้เบื้องต้นเกี่ยวกับภาษาPhp
ความรู้เบื้องต้นเกี่ยวกับภาษาPhpความรู้เบื้องต้นเกี่ยวกับภาษาPhp
ความรู้เบื้องต้นเกี่ยวกับภาษาPhp
 
Answer unit4.4.1
Answer unit4.4.1Answer unit4.4.1
Answer unit4.4.1
 
Answer unit4.3.2
Answer unit4.3.2Answer unit4.3.2
Answer unit4.3.2
 
Answer unit4.3.1
Answer unit4.3.1Answer unit4.3.1
Answer unit4.3.1
 
Answer unit4.2.4
Answer unit4.2.4Answer unit4.2.4
Answer unit4.2.4
 
Answer unit4.2.3
Answer unit4.2.3Answer unit4.2.3
Answer unit4.2.3
 
Answer unit4.2.2
Answer unit4.2.2Answer unit4.2.2
Answer unit4.2.2
 
Answer unit4.2.1
Answer unit4.2.1Answer unit4.2.1
Answer unit4.2.1
 
Answer unit4.1.1
Answer unit4.1.1Answer unit4.1.1
Answer unit4.1.1
 
Answer unit3.2
Answer unit3.2Answer unit3.2
Answer unit3.2
 
Answer unit3.1
Answer unit3.1Answer unit3.1
Answer unit3.1
 
Answer unit2.3
Answer unit2.3Answer unit2.3
Answer unit2.3
 
Answer unit1.3
Answer unit1.3Answer unit1.3
Answer unit1.3
 
Answer unit1.1
Answer unit1.1Answer unit1.1
Answer unit1.1
 
Program structure
Program structureProgram structure
Program structure
 

Unit3.1variables