PHP: Strings
ကျွန်တော်တို့တွေ PHP: Echo lesson မှာ string တွေ အတော်လေး သုံးခဲ့ကြပါတယ်။ ဒါပေမယ့် သူတို့အကြောင်း အသေးစိတ်မပြောခဲ့ပါဖူး။ ကျွန်တော်တို့ PHP code တွေ ရေးနေသမျှ ကာလပတ်လုံး string တွေနဲ့ ပတ်သက်နေရဦးမှာပါပဲ။ အဲဒါကြောင့် string တွေ အကြောင်း အခြေခံကျကျ နားလည်ထားဖို့ကလည်း အရေးကြီးပါတယ်။
PHP – String Creation
sting တစ်ခုကို အသုံးမပြုခင်မှာ သူ့ကို create လုပ်ရပါမယ်။ string တွေကို function ထဲမျာ direct ပဲ ထည့်သုံးသုံး၊ variable ထဲမှာ သိမ်းပြီးတော့မှပဲ သုံးသုံး အသုံးပြုနိုင်ပါတယ်။ အောက်က ဥပမာမှာ တထေရာထည်းတူတဲ့ string ၂ ခု ဖန်တီးပါမယ်။ ပထမတစ်ခုကို variable ထဲမှာ သိမ်းမယ်၊ နောက်တစ်ခုကို function ထဲမှာ direct ထည့်သုံးပါမယ်။
<html> <head> <title>My First PHP-enabled Page</title> <body> <?php $my_string = "I love PHP very much!"; echo "I love PHP very much!<br />"; echo $my_string; ?> </body> </html>
အပေါ်က ဥပမာမှာဆိုရင် ပထမ string ကို variable ထဲမှာ သိမ်းလိုက်တယ်။ ဒုတိယဟာကိုတော့ function ထဲမှာ direct ထည့်သုံးလိုက်တယ်။ တစ်ကြိမ်ထက် ပိုသုံးချင်တယ်ဆိုရင်တော့ variable ထဲထည့်ထားလိုက်ဖို့ သတိရလိုက်ပါ။
PHP – String Creation Single Quotes
အခုထိတော့ ကျနော်တို့တွေ string တွေ create လုပ်ရာမှာ double quote ကိုပဲ သုံးခဲ့ပါတယ်။ single quote (apostrophes) နဲ့သုံးလဲ မှန်တာပါပဲ။
$my_string = 'MyanmarTutorials - Let\'s collaborate!'; echo 'MyanmarTutorials - Let\'s collaborate!'; echo $my_string;
single quote နဲ့သုံးထားတဲ့ string ထဲမှာပဲ single quote ထပ်ပါလာရင်တော့ back slash (/) နဲ့ escape လုပ်ဖို့ မမေ့ပါနဲ့။
PHP: String Creation Double-Quotes
ကျနော်တို့တွေ string တွေ create လုပ်ဖို့အတွက် double quote တွေကို သုံးခဲ့ကြပီးပါပီ။ ဆက်ပြီးတော့လဲ string create လုပ်ရာမှာ double quote ကိုပဲ အဓိက သုံးသွားပါမယ်။ double quote တွေမှာ single quote မှာ သုံးလို့မရတဲ့ special escaped character တွေ အများကြီး သုံးလို့ရပါတယ်။ ဒီ escaped character တွေမှာလဲ back slash ကိုပဲ သုံးရပါတယ်။
$newline = "A newline is \n"; $return = "A carriage return is \r"; $tab = "A tab is \t"; $dollar = "A dollar sign is \$"; $doublequote = "A double-quote is \"";
escape လုပ်ဖို့ မလိုတဲ့ character ရှေ့မှာ back slash သွားထည့်ရင်တော့ output string မှာ အဲဒီ back slash ကြီးပါလာမှာ သတိထားပါ။
နောက်ပြီး ဒီ escape character တွေက web page မှာ output ပြတဲ့ အခါမှာ သိပ်အသုံးမဝင်ပါဖူး။ HTML က white space တွေကို ignore လုပ်သွားမှာပါ။ ဒါပေမယ့် file ထဲမှာ ရေးထည့်တဲ့ အခါမှာတော့ လူတွေ ဖတ်ရလွယ်အောင် ရှင်းရှင်းလင်းလင်းဖြစ်ဖို့ အကူအညီပေးပါလိမ့်မယ်။
PHP: String Creation Heredoc
အပေါ်မှာ သုံးခဲ့တဲ့ နည်းလမ်း ၂ ခုကတော့ သမားရိုးကျ နည်းလမ်းတွေပါ။ PHP မှာ multi line string တွေကို ဖန်တီးဖို့ syntax အသစ်တစ်ခု ပါ ပါသေးတယ်။ heredoc လို့ ခေါ်ပါတယ်။ သူ့မှာ quotation တွေ သုံးဖို့ မလိုပါဖူး။ ဒါပေမယ့် heredoc ကို သုံးရတာ ပိုခက်ပီး၊ code ကို syntax မှန်အောင် သေချာမရေးရင် error တက်ဖို့ လမ်းများပါတယ်။ heredoc နမူနာကြည့်ရအောင်။
$my_string = <<<TEST PHP is one of the World famous Server-side scripting Language! TEST; echo $my_string;
heredoc syntax သုံးတဲ့အခါမှာ အလွန်အရေးကြီးတဲ့ အချက်တွေရှိပါတယ်။ မှတ်ထားကြပါ။
- <<< နဲ့ နာမည် (identifier) တစ်ခုနဲ့ heredoc syntax ကို စရပါမယ်။ အပေါ်မှာ TEST ကို identifier အဖြစ်သုံးထားပါတယ်။
- စဖွင့်ခဲ့တဲ့ identifier နာမည်အတိုင်း အဆုံးမှာပြန်ထည့်ပီး semicolon နဲ့ ပိတ်ပေးရပါမယ်။ TEST; ပေါ့။
- အပိတ် TEST; ဟာ လိုင်းအသစ်မှာရှိရမှာဖြစ်ပီး၊ space ခြားထားတာ၊ indented (tab ခုန်) လုပ်ထားတာမျိုး မဖြစ်ရပါဖူး။
ဒီနေရာမှာ ကျနော်တို့က <br /> tag တွေ မထည့်ခဲ့တဲ့ အတွက် browser မှာ ပြရင်တော့ အောက်တစ်ကြောင်းကို ဆင်းဆင်းသွားမှာ မဟုတ်ပါဖူး။ ဒီလိုပဲ ပေါ်ပါလိမ့်မယ်။
PHP is the World famous Server-side scripting Language!
code ထဲကအတိုင်းဖြစ်ချင်ရင်တော့ စာကြောင်းအဆုံးတွေမှာ <br /> tag တွေလိုက်ထည့်ပေးရပါမယ်။
Facebook comments:



