PHP: Syntax

PHP syntax အေၾကာင္းမေျပာခင္ အရင္ဆံုး Syntax ရဲ႕ အဓိပၸါယ္ကို ဖြင့္ရေအာင္…

Syntax – The rules that must be followed to write properly structured code.

Syntax ဆိုတာ မွန္မွန္ကန္ကန္ ဖြဲ႔စည္းထားတဲ့ Code ေတြ ေရးရာမွာ လိုက္နာရမယ့္ စည္းမ်ဥ္းစည္းကမ္းေတြပါ။

PHP ရဲ႕ Syntax က တျခား Programming Language ေတြျဖစ္တဲ့ C, Java, Perl တို႔နဲ႔ အေတာ္ေလး ဆင္တူပါတယ္။ PHP code ေတြကို ေအာက္ပါ အတိုင္း <?php (open tag) နဲ႔ ?> (close tag) ေတြ အတြင္းမွာ ေရးရပါမယ္။

<?php
// Your PHP code goes here....
?>

တျခား Short tag, ASP style tag ေတြ သံုးလို႔ရေပမယ့္… configure လုပ္ဖို႔လိုပါတယ္။ အဲေတာ့ အခုလို Standard tag ကို သံုးတာ အေကာင္းဆံုး ျဖစ္ပါတယ္။

How to Save Your PHP Pages

HTML page ထဲမွာ PHP code ေတြထည့္ေရးထားပီး အဲဒီ page ကို web browser က မွန္မွန္ကန္ကန္ ဘာသာျပန္ ေပးႏိုင္ဖို႔အတြက္ Save လုပ္တဲ့ အခါမွာ ပံုမွန္ .html extension အစား .php extension နဲ႔ Save လုပ္ရမွာ ျဖစ္ပါတယ္။

Example PHP Page

Getting Started ထဲက PHP code ကိုပဲ ျပန္ၾကည့္ပါမယ္။

<html>
<head>
<title>My First PHP-enabled Page</title>
<body>
<?php
 echo "This is my first PHP-enabled page!";
?>
</body>
</html>

ဒီဥပမာမွာ echo ဆိုတာ PHP ရဲ႕ built-in function ပါ။ echo function ကို Web Browser မွာ စာေတြ (Text) ထုတ္ျပဖို႔အတြက္သံုးပါတယ္။ echo ေနာက္က Double Quote ထဲက စာသားေတြကို Browser မွာ ႐ိုက္ျပေပးမွာပါ။ (ေနာက္ပိုင္းမွာ echo အေၾကာင္း အေသးစိတ္ပါပါမယ္)

The Semicolon!

အေပၚက code မွာ သတိထားၾကည့္လိုက္ပါ။ PHP code line ရဲ႕အဆံုးမွာ ; (semicolon) တစ္ခုပါပါတယ္။ ; (semicolon) က PHP statement တစ္ခုဆံုးသြားၿပီဆိုတဲ့ သေဘာပါ။ PHP code တစ္ေၾကာင္းဆံုးတိုင္း ; (semicolon) ပိတ္ေပးဖို႔ မေမ့ပါနဲ႔။ အေပၚက ဥပမာထဲက This is my first PHP-enabled page! ဆိုတဲ့ စာသားကို ထပ္ခါထပ္ခါ ျပခ်င္တယ္ဆိုရင္ echo “This is my first PHP-enabled page!” ဆိုပီး ထပ္ထည့္ ရပါမယ္။ တစ္ေၾကာင္းဆံုးတိုင္း ; (semicolon) ပိတ္ေပးရပါမယ္။

<html>
<head>
<title>My First PHP-enabled Page</title>
<body>
<?php
 echo "This is my first PHP-enabled page!<br />";
 echo "This is my first PHP-enabled page!<br />";
 echo "This is my first PHP-enabled page!<br />";
 echo "This is my first PHP-enabled page!<br />";
?>
</body>
</html>
The First PHP Page 2

The First PHP Page 2

echo function ထဲမွာ HTML code ေတြ ထည့္ေရးလို႔ရတာ သတိျပဳပါ။

White Space

HTML code လိုပါပဲ PHP code ေတြမွာလဲ White Space ေတြကို လစ္လ်ဴရႈပါတယ္။ ဆိုလိုတာက PHP code တစ္ေၾကာင္းပီးသြားတယ္။ ေနာက္တစ္ေၾကာင္းမစခင္မွာ စာေၾကာင္းအလြတ္ေတြ ဘယ္ေလာက္ျခားျခား အဲဒီ အလြတ္ေတြကို ေက်ာ္သြားမွာပါ။ ေနာက္ပီး Code ေတြကို သပ္ရပ္ေအာင္ Tab ႏွိပ္ပီး Indent လုပ္ထားရင္လဲ အဲဒီ Tab ခုန္ထားတဲ့ Space ေတြကိုေက်ာ္သြားမွာပါ။

PHP Whitespace

PHP Whitespace

Embedding PHP in HTML

HTML code ေတြနဲ႔ PHP code ေတြ ေရာေမႊပီးေရးၾကည့္မယ္။ firstpage.php ကိုပဲ ျပင္ေရးလိုက္ပါ။

<html>
<head>
<title>My First PHP-enabled Page</title>
<body>
<?php
 echo "<p>This is some text.</p>";
?>
<p>Some of this text is static, <?php echo "but this sure isn't!"; ?></p>
<?php echo "<p>"; ?>
This text is enclosed in paragraph tags that were generated by PHP.
<?php echo "</p>"; ?>
</body>
</html>
Embedding PHP into HTML

Embedding PHP into HTML

Trace ေတာ့လိုက္မျပေတာ့ဖူး.. လြယ္လြယ္ေလးပါ.. နားလည္ၾကမွာပါ။ :)

Facebook comments:

Leave a comment


*