ကၽြန္ေတာ္တို႔ေတြ echo function ကို ပီးခဲ့တဲ့ lesson ေတြမွာ ေတြ႔ခဲ့ပီးပါပီ။ echo function ကို Browser မွာ Text output ထုတ္ျပဖို႔ သံုးပါတယ္။ အမ်ားဆံုးသံုးရမယ့္ function ပါပဲ။ အဲဒါေၾကာင့္ echo အေၾကာင္း ေသေသခ်ာခ်ာ ေလ့လာၾကည့္ရေအာင္…
Outputting a String String ေတြ output ထုတ္ျပဖို႔အတြက္ echo function မွာ string variable or quoted text ေတြ သံုးရပါတယ္။ ေအာက္မွာ ဥပမာကို ၾကည့္ပါ။
<html><br /><br /><head><br /><br /><title>My First PHP-enabled Page</title><br /><br /><body><br /><br /><?php<br /><br />$myString = "Hello!";<br /><br />echo $myString;<br /><br />echo "<h5>I love using PHP!</h5>";<br /><br />?><br /><br /></body><br /><br /></html>
Echoing String
Echoing String
$myString ထဲမွာ “Hello!” ကို ထည့္ထားေတာ့ echo $myString; ဆိုတာက echo “Hello!”; ျပန္ျဖစ္သြားတယ္ေလ။ ျမင္မွာပါေနာ္။ ေနာက္ echo မွာေတာ့ Header 5 ျဖစ္ခ်င္လို႔ <h5> ထည့္ေပးလိုက္တာ။ အဲမွာ HTML syntax ကို မွန္မွန္ကန္ကန္ေရးဖို႔လိုတယ္ေနာ္။
Careful When Echoing Quotes!
PHP နဲ႔ HTML output ထုတ္ရတာ အေတာ္ေလးကို လြယ္ပါတယ္။ ဒါေပမယ့္ echo function က quote ( ‘ or ” ) နဲ႔ string ရဲ႕ အစနဲ႔ အဆံုးကို ခြဲတာျဖစ္ပါတယ္။ HTML code ေတြ၊ ဒါမွမဟုတ္ တျခားစာသားေတြမွာ quote ေတြပါလာရင္ေတာ့ သတိထားရေတာ့မယ္။ echo နဲ႔ output ထုတ္မယ့္ string မွာ quotations ေတြ ပါလာခဲ့ရင္ ေအာက္ပါ ၃ ခ်က္ထဲက တစ္္ခုခုကို လိုက္နာပါ။
* ျဖစ္ႏိုင္ရင္ quote ေတြကို string ထဲမွာ မသံုးပါနဲ႔။
* String ထဲမွာပါမယ့္ quote ကို Backslash ( \ ) နဲ႔ escape လုပ္ပါ။ escape လုပ္ဖို႔ကေတာ့ quote ရဲ႕ေရွ႕မွာ backslash ကပ္ထည့္ေပးလိုက္႐ံုပါပဲ။ e.g: \”
* String ထဲက quote ကို single quote (apostrophes) သံုးပါ။
ေအာက္က ဥပမာမွာ ဘယ္ဟာမွားတယ္၊ ဘယ္ဟာမွန္တယ္ဆိုတာ ၾကည့္ၾကည့္ပါ။
<html><br /><br /><head><br /><br /><title>My First PHP-enabled Page</title><br /><br /><body><br /><br /><?php<br /><br />// This won't work because of the quotes around specialH5!<br /><br />//echo "<h5>I love using PHP!</h5>";<br /><br />// OK because we escaped the quotes!<br /><br />echo "<h5 class=\"specialH5\">I love using PHP!</h5>";<br /><br />// OK because we used an apostrophe '<br /><br />echo"<h5 class='specialH5'>I love using PHP!</h5>";<br /><br />?><br /><br /></body><br /><br /></html>
အဲေတာ့ output string မွာ quotation ပါလာခဲ့ရင္ single quote ကိုသံုးရပါမယ္။ double quote ပါလာတယ္ဆိုရင္ေတာ့ backslash နဲ႔ escape လုပ္ကိုလုပ္ရပါမယ္။ မွတ္ထားပါ။
Echoing Variables
PHP မွာ variable ေတြကို output ထုတ္ျပရတာ ေတာ္ေတာ္ကိုလြယ္ပါတယ္။ quote ေတြမလိုပါဖူး။ ေနာက္ဆံုး variable ထဲမွာ ဘာ data မွ မပါလဲ ျပႆနာမရွိပါဖူး။ ေအာက္မွာ နမူနာၾကည့္ပါ။
<html><br /><br /><head><br /><br /><title>My First PHP-enabled Page</title><br /><br /><body><br /><br /><?php<br /><br />$my_string = "Hello Bob. My name is: ";<br /><br />$my_number = 4;<br /><br />$my_letter = a;<br /><br />echo $my_string;<br /><br />echo $my_number;<br /><br />echo $my_letter;<br /><br />?><br /><br /></body><br /><br /></html>
Echoing Variables
Echoing Variables
is: ရဲ႕ေနာက္မွာ space ျခားထားေတာ့ space ပါ ထည့္ျပသြားတာ သတိထားၾကည့္ပါ။
Echoing Variables and Text Strings
echo function မွာ variable ေတြနဲ႔ text string ေတြ ေပါင္းေရးလို႔ရပါေသးတယ္။ အဲလိုေပါင္းေရးရင္ echo statement ေတြ အမ်ားႀကီး ေရးစရာမလိုေတာ့ဘူးေပါ့။ PHP မွာ variable ေတြနဲ႔ text string ေတြကို period ( . ) နဲ႔ ဆက္ရပါတယ္။ ေအာက္မွာ နမူနာၾကည့္ပါ…
<html><br /><br /><head><br /><br /><title>My First PHP-enabled Page</title><br /><br /><body><br /><br /><?php<br /><br />$my_string = "Hello Bob. My Name is: ";<br /><br />$newline = "<br />";<br /><br />echo $my_string . "Bobetta" . $newline;<br /><br />echo "Hi, I'm Bob. Who are you? " . $my_string . $newline;<br /><br />echo "Hi, I'm Bob. Who are you? " . $my_string . "Bobetta";<br /><br />?><br /><br /></body><br /><br /></html>
Echoing Variables and Text Strings
Echoing Variables and Text Strings
အေပၚမွာျပထားသလိုပဲ period ခံပီးဆက္တာ ႀကိဳက္သေလာက္ ဆက္လို႔ရပါတယ္။ ဒီနည္းနဲ႔ ၂ ခု (သို႔) ၂ ခုထက္ပိုတဲ့ string or variable ေတြ ဆက္တာကို Concatenation လို႔ေခၚပါတယ္။ Concatenation အေၾကာင္းနဲ႔ တျခား String manipulation method ေတြကို Strings အခန္းမွာ ဆက္ေျပာပါမယ္။