PHP: Variables

ဘယ္ Programming Language မွာမဆို Variable ေတြကေတာ့ မပါမျဖစ္ပါပဲ။ ဒီေနရာမွာေတာ့ Variable အေၾကာင္းကို အေသးစိတ္ မရွင္းေတာ့ပါဖူး။ လိုရင္ပဲေျပာပါမယ္။

Variable ဆိုတာ တန္ဖိုးေတြ သိမ္းထားဖို႔ System Memory ေပၚကေနရာပါပဲ။ တန္ဖိုးဆိုတာ စာသားေတြ (Text string) ဒါမွမဟုတ္ ဂဏန္း (Number) စတာေတြပါ။ Program ေရးတဲ့အခါမွာ User ေတြဆီကေန အခ်က္အလက္ယူရတာမ်ိဳးရွိပါတယ္။ ဥပမာ – Email program ဆိုပါစို႔။ ဘယ္သူ႔ mail ေတြ ျပေပးရမွန္းမသိတဲ့ အတြက္ က်ေနာ္တို႔ကို username တာင္းပါမယ္။ အဲဒီမွာ က်ေနာ့ဟာဆိုရင္ တစ္မ်ိဳး၊ တစ္ျခားတစ္ေယာက္ရဲ႕ဟာဆိုရင္ တစ္မ်ိဳးျဖစ္မွာပါ။ အခုလို ႀကိဳမွန္းထားလို႔ မရတဲ့ တန္ဖိုးေတြကို Variable ထဲမွာ ထည့္သိမ္းရပါတယ္။ PHP မွာေတာ့ Variable တစ္ခုကို ေအာက္ပါအတိုင္း သတ္မွတ္ႏိုင္ပါတယ္။

$variable_name = Value;

PHP variable ေတြကို $ (dollar sign) နဲ႔စေပးရပါတယ္။ $ ထည့္ဖို႔ေမ့သြားရင္ အလုပ္လုပ္မွာမဟုတ္ပါဖူး။ PHP စေရးခါစလူေတြ ခဏခဏ ေမ့တတ္ပါတယ္။ သတိထားပါ။ PHP variable name ေတြဟာ case-sensitive ျဖစ္ပါတယ္။ အႀကီးအေသးမွားလို႔မရပါဘူး။ $a_number နဲ႔ $A_number ဆိုရင္ PHP မတူတဲ့ variable ၂ ခုလို႔ သတ္မွတ္မွာပါ။

Storing Values in a Variable

PHP variable မွာ ေအာက္ပါ Datatype ေတြကို သိမ္းလို႔ရပါတယ္။

  • String: Alphanumeric characters, such as sentences or names
  • Integer: A numeric value, expressed in whole numbers
  • Float: A numeric value, expressed in real numbers
  • Boolean: Evaluates to TRUE or FALSE (1 = TRUE and 0 = FALSE)
  • Array: An indexed collection of data
  • Object: A collection of data and methods

PHP က loosely typed language ပါ။ ဆိုလိုတာက variable ေတြရဲ႕ datatype ကို သတ္မွတ္ေပးစရာမလိုပါဖူး။ C, Java တို႔လို strictly typed language ေတြမွာဆိုရင္ေတာ့ variable တစ္ခု စေၾကျငာကတည္းက သူဟာ integer လား၊ string လား သတ္မွတ္ေပးရပါတယ္။ PHP မွာေတာ့ ထည့္တဲ့ တန္ဖိုးအေပၚမူတည္ပီး datatype ကို သူ႔အလိုလိုပဲ သတ္မွတ္ေပးသြားပါတယ္။ ဆိုလိုတာက.. 25 ထည့္လိုက္ရင္ interger ျဖစ္သြားမယ္၊ 23.5 ဆိုရင္ float ျဖစ္သြားမယ္၊ “Goo Goo” ဆိုရင္ string ျဖစ္သြားမယ္။

Variable Example

<?php
$hello = "Hello PHP!";
$a_number = 4;
$anotherNumber = 7;
?>

PHP Variable Naming Conventions

Variable ေတြကို နာမည္ေပးတဲ့အခါမွာ လိုက္နာရမည့္ စည္းကမ္းေတြရွိပါတယ္။

  • PHP variable ေတြဟာ letter (စာလံုး) သို႔မဟုတ္ Underscore ( _ ) နဲ႔ စရပါမယ္။
  • PHP variable ေတြမွာ a-z, A-Z, 0-9 နဲ႔ _ ပဲ သံုးလို႔ရပါမယ္။
  • စကားလံုးတစ္လံုး (one word) ထက္ပိုတဲ့ နာမည္ေတြကို underscore သံုးပီး ျခားရပါမယ္။ e.g: $my_variable
  • စကားလံုးတစ္လံုး (one word) ထက္ပိုတဲ့ နာမည္ေတြကို ဒုတိယေျမာက္စကားလံုးကေနစပီး ေရွ႕ဆံုး စာလံုးကို အႀကီးေျပာင္းတဲ့ ပံုစံနဲ႔လဲ ေရးႏိုင္ပါတယ္။ e.g: $myVariable

(ေနာက္ပိုင္းမွာ variable ေတြကို လက္ေတြ႔သံုးရေတာ့မွာမို႔ ဒီမွာေတာ့ ေၾကျငာပံု၊ နာမည္ေပးပံုေလာက္ပဲ ေျပာခဲ့ပါဦးမယ္)

Facebook comments:

Leave a comment


*