PHP: Operators
Operator တွေကို Variable တွေ Value တွေရဲ့ တွက်ချက်မှုတွေ ကိုင်တွယ် ဖြေရှင်းဖို့အတွက် Programming Language တိုင်းမှာ အသုံးပြုပါတယ်။ ကျနော်တို့တွေ String တွေကို ဆက်ပေးတဲ့ (string concatenation operator) “.” (period) နဲ့ တန်ဖိုးတွေ ထည့်ပေးတဲ့ (Assignment Operator) “=” (equal) ကို Echo Lesson မှာ မြင်ဖူးပီးပါပြီ။
PHP မှာ သုံးနိုင်တဲ့ Operator တွေ အများကြီး ရှိပါတယ်။ PHP Operator တွေကို အလွယ်တကူ လေ့လာနိုင်အောင်လို့ အောက်ပါအတိုင်း အမျိုးအစား ခွဲခြားနိုင်ပါတယ်။
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- String Operators
- Combination Arithmetic & Assignment Operators
Assignment Operators
Assignment Operator တွေကို Variable တစ်ခုမှာ Value တစ်ခု ထည့်ဖို့ (သို့) Variable တစ်ခုမှာ တခြား Variable နောက်တစ်ခုကို ထည့်ဖို့အတွက် သုံးပါတယ်။ PHP မှာ Assignment Operator အတွက် “=” (equal character) ကို သုံးပါတယ်။ ဥပမာ -
- $my_var = 5;
- $another_var = $my_var;
အခုဆိုရင် $my_var နဲ့ $another_var နှစ်ခုစလုံးမှာ 4 (value) ရှိနေမှာပါ။ Assignment operator ကို Arithmetic operator တွေနဲ့လဲ တွဲသုံးနိုင်ပါတယ်။
Arithmetic Operators
| Operator | English | Example |
|---|---|---|
| + | Addition | 2 + 4 |
| - | Subtraction | 6 – 2 |
| * | Multiplication | 5 * 3 |
| / | Division | 15 / 3 |
| % | Modulus | 43 % 10 |
Example:
<html> <head> <title>My First PHP-enabled Page</title> <body> <?php $addition = 2 + 4; $subtraction = 6 - 2; $multiplication = 5 * 3; $division = 15 / 3; $modulus = 5 % 2; echo "Perform addition: 2 + 4 = " . $addition . "<br />"; echo "Perform subtraction:: 6 - 2 = " . $subtraction . "<br />"; echo "Perform multiplication: 5 * 3 = " . $multiplication . "<br />"; echo "Perform division: 15 / 3 = " . $division . "<br />"; echo "Perform modulus: 5 % 2 = " . $modulus . "<br />Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1."; ?> </body> </html>
Division operator ကတော့ အဖြေအဖြစ် စားလဒ်ကို ပေးပါတယ်။ Modulus operator ကတော့ အကြွင်းကို ပေးပါတယ်။
Comparison Operators
Comparison Operator တွေကို Variable (သို့) Value တွေ နှိုင်းယှဉ်တဲ့ နေရာမှာ အသုံးပြုကြပါတယ်။ (Comparison Operator တွေရဲ့ အသုံးကို If Statement Lesson ရောက်မှပဲ ပြပါတော့မယ်) Comparison operator တွေကို မှန်တယ်၊ မှားတယ် ဆိုတဲ့ အခြေအနေအရ လုပ်ဆောင်ရမယ့် လုပ်ဆောင်ချက်တွေမှာ true or false တန်ဖိုးတွေ အဖြေပြန်ထုတ်ပေးဖို့အတွက် Conditional statement တွေရဲ့ အတွင်းမှာ သုံးရပါတယ်။ အောက်မှာ အရေးကြီးတဲ့ PHP comparison operator တွေပြပေးပါမယ်။ $x = 4, $y = 5 အနေနဲ့ သုံးပါမယ်။
| Operator | English | Example | Result |
|---|---|---|---|
| == | Equal To | $x == $y | false |
| != | Not Equal To | $x != $y | true |
| < | Less Than | $x < $y | true |
| > | Greater Than | $x > $y | false |
| <= | Less Than or Equal To | $x <= $y | true |
| >= | Greater Than or Equal To | $x >= | false |
String Operators
String Operator ( “.” ) ကို ကျနော်တို့တွေ Echo Lesson မှာ သုံးဖူးပီးပါပြီ။ String တွေကို ဆက်ပေးတာပါ။ String Concatenation Operator လို့လဲ ခေါ်ပါတယ်။
Example:
$a_string = "Hello"; $another_string = " Billy"; $new_string = $a_string . $another_string; echo $new_string . "!";
Output:
Hello Billy!
Combination Arithmetic & Assignment Operators
Program ရေးသားရာမှာ Variable တစ်ခုထဲကို နောက်ထပ်တန်ဖိုး ထပ်ပေါင်းတာတို့၊ နှုတ်တာတို့ ခဏတိုင်း လုပ်ရပါတယ်။ အမြင်သာဆုံး ဥပမာကတော့ Counter ပါ။
$counter = $counter + 1;
အဲဒါကို ဒီလို အတိုပုံစံ ရေးလို့ရပါတယ်။
$counter += 1;
အခုလို Assignment နဲ့ Arithmetic operator တွေကို ပေါင်းပီး အတိုပုံစံရေးသားနိုင်ပါတယ်။ အများဆုံးသုံးတာကတော့ “+=” နဲ့ “-=” ပဲ ဖြစ်ပါတယ်။ အောက်မှာ အသုံးများတဲ့ Combination operator တွေကို ပြပေးပါမယ်။
| Operator | English | Example | Equivalent Operation |
|---|---|---|---|
| += | Plus Equals | $x += 2; | $x = $x + 2; |
| -= | Minus Equals | $x -= 4; | $x = $x – 4; |
| *= | Multiply Equals | $x *= 3; | $x = $x * 3; |
| /= | Divide Equals | $x /= 2; | $x = $x / 2; |
| %= | Modulus Equals | $x %= 5; | $x = $x % 5; |
| .= | Concatenate Equals | $my_str .= “hello”; | $my_str = $my_str . “hello”; |
Pre/Post-Increment & Pre/Post-Decrement
ဒီ Operator တွေကတော့ Variable တစ်ခုမှာ တစ်ပေါင်း၊ တစ်နှုတ် လုပ်တဲ့အခါမှာ အတိုပုံစံနဲ့သုံးတာပါ။ Variable တစ်ခုမှာ တစ်တိုးဖို့အတွက် “++” (increment operator)၊ တစ်လျှော့ဖို့အတွက် “–” (decrement operator) သုံးရပါတယ်။
- $x++; ဆိုတာ $x += 1; (သို့) $x = $x + 1; နဲ့ အတူတူပါပဲ။
- $x–; ဆိုတာ $x -= 1; (သို့) $x =$x – 1; နဲ့ အတူတူပါပဲ။
ဒီ Operator တွေကို အတိုပုံစံနဲ့ သုံးတဲ့အပြင် code line တစ်ခု execute မလုပ်ခင် အတိုးအလျှော့ လုပ်ရာမှာနဲ့ execute လုပ်အပြီးမှာ အတိုးအလျှော့ လုပ်ရာမှာလဲ အသုံးများပါတယ်။ အောက်ပါ ဥပမာမှာ ကွဲပြားပုံကို လေ့လာကြည့်ပါ။
<html> <head> <title>My First PHP-enabled Page</title> <body> <?php $x = 4; echo "The value of x with post-plusplus = " . $x++; echo "<br />The value of x after the post-plusplus is " . $x; $x = 4; echo "<br />The value of x with pre-plusplus = " . ++$x; echo "<br />The value of x after the pre-plusplus is " . $x; ?> </body> </html>
ပထမ echo က $x++ မှာ တန်ဖိုး တိုးသွားခြင်း မရှိပါဖူး။ post increment ဖြစ်တဲ့အတွက် အဲဒီ echo လိုင်း execute လုပ်သွားပြီးမှ တစ်တိုးသွားတာပါ။ အဲဒီအတွက် ဒုတိယ echo လိုင်းမှာတော့ $x ရဲ့ တန်ဖိုးက 5 ဖြစ်နေပါပီ။ pre increment ++$x ပါတဲ့ echo လိုင်းမှာတော့ တစ်ခါထဲ တစ်တိုးပြီး execute လုပ်သွားတဲ့ အတွက် 5 လို့ ပြပါတယ်။ နောက်ဆုံး echo မှာ $x ကို ဘာမှ လုပ်မထားတဲ့ အတွက် 5 ပါပဲ။ Decrement operator ( “–” ) ကလဲ အတူတူ ပါပဲ။ သူကတော့ တစ်လျှော့ ပေးမှာပေါ့။
Facebook comments:




