Monday, 2024-05-13, 2:55 AM
ဟက္ကာဖိုရမ္
Main Registration Login
Welcome, Guest · RSS
[ New messages · Members · Forum rules · Search · RSS ]
  • Page 1 of 1
  • 1
Forum » WEB DEVELOPMENT » PHP » PHP: Strings
PHP: Strings
ဟတ္ကာDate: Tuesday, 2010-10-12, 7:33 PM | Message # 1
Major
Group: Administrators
Messages: 91
Reputation: 0
Status: Offline
ကၽြန္ေတာ္တို႔ေတြ 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 Creation

String Creation

အေပၚက ဥပမာမွာဆိုရင္ ပထမ 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 ေတြလိုက္ထည့္ေပးရပါမယ္။

 
Forum » WEB DEVELOPMENT » PHP » PHP: Strings
  • Page 1 of 1
  • 1
Search:

Powered by uCoz