PHP တွင် ထပ်ခါထပ်ခါလုပ်ဆောင်ချက်များကို အကောင်းဆုံးဖြစ်အောင်ပြုလုပ်ခြင်း
မကြာခဏဆိုသလို စတင်လေ့လာနေတဲ့ ပရိုဂရမ်မာတွေဟာ အတွေးမပါဘဲ တူညီတဲ့ function တစ်ခုကို အကြိမ်ကြိမ်ခေါ်သုံးပြီး အရင်းအမြစ်တွေကို ဖြုန်းတီးတတ်ကြပါတယ်။
ဥပမာတစ်ခုကို ကြည့်ရအောင်။ အောက်ပါကုဒ်ကို စဉ်းစားကြည့်ပါ။
<?php
$arr = [1, 2, 3, 4, 5];
if (count($arr) >= 1 and count($arr) <= 3) {
}
?>
ဒီကုဒ်ထဲမှာ ဘာမှားနေလဲ? အမှန်တော့ ကျွန်တော်တို့ဟာ
တူညီတဲ့အလုပ်ကို နှစ်ကြိမ်လုပ်နေတယ် - count($arr) ကို အသုံးပြုပြီး
array ရဲ့အရှည်ကို တွက်နေတယ်။ ဒီ
လုပ်ဆောင်ချက်ဟာ အချိန်ကုန်ပြီး တစ်ကြိမ်တည်း
လုပ်ဆောင်ပြီး ရလဒ်ကို variable တစ်ခုမှာသိမ်းထားကာ
ပြီးရင် လိုအပ်တဲ့နေရာတွေမှာ အဲဒီ
variable ကို ပြန်သုံးသင့်ပါတယ်၊ ဒီလိုမျိုးပါ။
<?php
$arr = [1, 2, 3, 4, 5];
$len = count($arr);
if ($len >= 1 and $len <= 3) {
}
?>
အောက်ပါကုဒ်ကို အကောင်းဆုံးဖြစ်အောင်ပြုလုပ်ပါ။
<?php
if (date('Y') >= 2018 and date('Y') <= 2020) {
echo 'year ' . date('Y') . ' is suitable';
} else {
echo 'year ' . date('Y') . ' is not suitable';
}
?>
အောက်ပါကုဒ်ကို အကောင်းဆုံးဖြစ်အောင်ပြုလုပ်ပါ။
<?php
$password = 'abcde';
if (strlen($password) >= 2 and strlen($password) <= 10) {
echo 'password length is acceptable';
} else {
echo 'invalid password length';
}
?>