ရိုးရိုးလေး ရှင်းပြမယ်
PHP ကို ရိုးရိုးလေး ပြောရရင်
PHP ဆိုတာ ဝင်လာတဲ့သူရဲ့ browser ထဲမှာ မဟုတ်ဘဲ ကိုယ်ထိန်းချုပ်ထားတဲ့ ကွန်ပျူတာ (server) ပေါ်မှာ run တဲ့ programming language တစ်ခုပါ။ ဝင်လာတဲ့သူက page တစ်ခုကို ဖွင့်တဲ့အခါ server က သင့်ရဲ့ PHP file ကို run ပြီး အဲဒီ page မှာ ဘာပြရမလဲ ဆုံးဖြတ်ကာ ပြီးသွားတဲ့ ရလဒ်ကိုပဲ ပြန်ပို့ပေးပါတယ်။
စားသောက်ဆိုင်တစ်ခုရဲ့ မီးဖိုချောင်လို မြင်ကြည့်ပါ။ Customer (browser) က မှာလိုက်ရင် ချက်ပြီးသား ဟင်းပွဲတစ်ပွဲပဲ စားပွဲပေါ် ရောက်လာပါတယ်၊ ချက်နည်းစာအုပ် (သင့်ရဲ့ PHP source code) ကိုတော့ ဘယ်တော့မှ မမြင်ရပါဘူး။ ချက်ပြုတ်တာကို server ဘက်မှာ လုပ်တဲ့အတွက် database ထဲက data ကြည့်တာ၊ password စစ်တာတွေကို ဝင်ကြည့်သူ ဘယ်တော့မှ မမြင်ရတဲ့ နေရာမှာ PHP က လုပ်ပေးနိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ရိုးရိုး HTML page က လူတိုင်းအတွက် အတူတူပဲ
စားသောက်ဆိုင်တစ်ခုရဲ့ website မှာ menu ပစ္စည်း ၅၀၀ ရှိတယ်ဆိုပါစို့။ HTML သက်သက်နဲ့ဆိုရင် ပစ္စည်းတစ်ခုစီအတွက် file တစ်ခုစီ လက်နဲ့ ရေးရပါမယ်။ ဈေးနှုန်းတစ်ခု ပြောင်းချင်ရင် သက်ဆိုင်ရာ file ကို ဖွင့်ပြင်ရပါမယ်။ ဆိုင်ကို ပြန်လာတဲ့ customer ကို နာမည်နဲ့ နှုတ်ဆက်ဖို့ကတော့ မဖြစ်နိုင်ပါဘူး၊ page က ဘယ်သူဝင်ဝင် အတူတူပဲ ပြနေလို့ပါ။
HTML file ဆိုတာ ရိုက်ပြီးသား ဓာတ်ပုံတစ်ပုံလိုပါ။ ဝင်လာတဲ့သူ ဘယ်သူလဲ၊ ဘယ်အချိန်လဲ၊ data ကအခု ဘာတွေလဲ ဆိုတာကို သူက မသိပါဘူး။ ဒါကြောင့် request တစ်ခုဝင်လာတိုင်း နိုးထပြီး data ကို ရှာကာ HTML ကို တစ်ခါတည်း အသစ်ရေးပေးမယ့် code တစ်ခု လိုလာပါတယ်။
<!-- one hand-written file per menu item: product-1.html ... product-500.html -->
<h1>Tea leaf salad</h1>
<p>Price: 3500 MMK</p>
<p>Welcome back, Aye Aye!</p>
<!-- the same greeting for every visitor, and 500 files to edit when prices change -->ဘာကြောင့် ဒါက ပြဿနာလဲ
Page တစ်ခုချင်းကို လက်နဲ့ ရေးရင် ပြင်ရမယ့် နေရာတွေလည်း များလာပြီး စာလုံးပေါင်း အမှားလည်း ပိုဖြစ်လာပါတယ်။ PHP နဲ့ဆိုရင် template တစ်ခုတည်းကို data နဲ့ ဖြည့်ပြီး HTML ကို request တိုင်းမှာ ထုတ်ပေးလို့ ရပါတယ်။ ဒါကြောင့် ဈေးနှုန်းကို တစ်နေရာတည်းမှာ ပြင်လိုက်ရင် page ၅၀၀ လုံးမှာ အပြောင်းအလဲ ပါသွားပါတယ်။
အဓိက သဘောတရား: request တိုင်းအတွက် PHP က server ပေါ်မှာ page ကို ဆောက်ပေးတယ်
Web page တစ်ခုကို ဖွင့်တဲ့အခါ browser နဲ့ server တို့က HTTP နဲ့ စကားပြောကြပါတယ်။ Browser က request (ဘယ် URL လဲ၊ ဘယ် method လဲ ဆိုတာ ရေးထားတဲ့ စာတစ်စောင်) ကို ပို့ပြီး server က response တစ်စောင် ပြန်ပို့ပါတယ်။ Server ဆိုတာ Apache, Nginx လို web server software ပါ၊ PHP ကိုယ်တိုင်ပါတဲ့ development server သေးသေးလေးလည်း ရှိပါတယ်။ Request က `.php` file တစ်ခုကို ညွှန်ရင် web server က အဲဒီ file ကို PHP engine ဆီ လွှဲပေးပါတယ်။
Browser က request ပို့တယ်
`GET /shop.php` လိုမျိုး request ထဲမှာ URL, method နဲ့ header တွေ ပါပါတယ်။
PHP engine က file ကို အပေါ်ကနေ အောက်အထိ run တယ်
Statement တစ်ကြောင်းချင်းစီကို အစီအစဉ်အတိုင်း လုပ်ပြီး ကြားထဲမှာ database ကို မေးတာ၊ တွက်ချက်တာတွေ လုပ်နိုင်ပါတယ်။
PHP က print လုပ်တာတွေက response body ဖြစ်တယ်
`echo` နဲ့ ထုတ်ပေးတဲ့ စာသား (အများအားဖြင့် HTML) ကို server က status code နဲ့ header တွေနဲ့အတူ ပြန်ပို့ပါတယ်။
Browser က ရလဒ်ကိုပဲ မြင်တယ်
PHP source code ကို ဘယ်တော့မှ မပို့ပါဘူး၊ ဒါကြောင့် browser ရဲ့ View Source မှာ HTML ကိုပဲ တွေ့ရပါတယ်။
One request, from click to page
-------------------------------
Browser Web server + PHP Database / files
------- ---------------- ----------------
| GET /shop.php | |
|--------------------->| run shop.php top to bottom |
| |--------------------------------->| query
| |<---------------------------------| rows
| | build the HTML with echo |
|<---------------------| 200 OK + HTML (no PHP code) |
| render the page | |PHP က web server မပါဘဲလည်း အလုပ်လုပ်နိုင်ပါတယ်။ Terminal ထဲမှာ `php script.php` လို့ ရိုက်ရင် language အတူတူပါပဲ၊ output ကို browser ဆီ မပို့ဘဲ terminal မှာပဲ ပြပေးတာသာ ကွာပါတယ်။ ဒီ course ရဲ့ အစပိုင်းမှာ ဒီနည်းကို သုံးပါမယ်၊ ဒါမှ server မသတ်မှတ်ရသေးခင် language ကိုယ်တိုင်ကို အရင်လေ့လာလို့ ရမှာပါ။ Form, session လို web နဲ့ တိုက်ရိုက်ဆိုင်တဲ့ အပိုင်းတွေကိုတော့ browser ပါလာတဲ့အခါ နောက်မှ ထည့်သင်ပါမယ်။
နောက်ထပ် ခွဲခြားမှတ်သားစရာတစ်ခုက နောက်ပိုင်း ရှုပ်ထွေးမှု လျော့စေပါလိမ့်မယ်။ 'PHP' ဆိုတဲ့ နာမည်က သင်ရေးတဲ့ language၊ အဲဒါကို run ပေးတဲ့ engine နဲ့ engine ကို အပြင်ကမ္ဘာနဲ့ ချိတ်ပေးတဲ့ SAPI (server API) ကို အားလုံး ခေါ်ဝေါ်တာပါ။ Engine တစ်ခုတည်းကို terminal က (`cli` SAPI)၊ Nginx နောက်က PHP-FPM က ဒါမှမဟုတ် Apache က စတင်နိုင်ပါတယ်။ စတင်ပုံ တစ်မျိုးစီက ကိုယ်ပိုင် php.ini ထဲက setting တွေ မတူတာကို ဖတ်နိုင်ပါတယ်။ နောက် lesson မှာ ဒီအစိတ်အပိုင်းတွေကို တစ်ခုချင်း ကြည့်ပါမယ်။
- Browser က request ပို့
- .php file ကို PHP ဆီ လွှဲ
- PHP run ပြီး page ကို print
- Browser က output ကိုပဲ ရ
PHP code က server ပေါ်မှာပဲ ရှိပြီး browser ဆီ ရောက်တာက output ပါ။ ဒါကြောင့် browser ထဲက button နှိပ်တာနဲ့ PHP function ကို တိုက်ရိုက် မခေါ်နိုင်ပါဘူး၊ request အသစ်တစ်ခု ပို့ရပါတယ်။
ယေဘုယျပုံစံ: PHP script တစ်ခု
<?php
declare(strict_types=1);
function name(Type $param): ReturnType
{
return $result;
}
echo name($argument), PHP_EOL;`<?php` နဲ့ စပါတယ်။ `declare(strict_types=1);` က type စစ်ဆေးမှုကို တင်းကြပ်စေပါတယ်။ Function တစ်ခုမှာ parameter type တွေနဲ့ return type ပါပြီး `echo` က ရလဒ်ကို output ဆီ ပို့ပါတယ်။ `PHP_EOL` က စာကြောင်းအဆုံးမှာ line break တစ်ခု ထည့်ပေးပါတယ်။
ပထမဆုံး PHP script လေး
အောက်က script ကို `welcome.php` ဆိုပြီး သိမ်းပါ။ Terminal ထဲမှာ အဲဒီ folder ကို ဝင်ပြီး `php welcome.php` လို့ ရိုက်ရင် run ပါလိမ့်မယ်။ Setup ကို lesson ၃ မှာ သေသေချာချာ လုပ်ပါမယ်၊ အခုတော့ code ကို ဖတ်ပြီး ဘာထွက်လာမလဲ ကြိုမှန်းကြည့်ပါ။ နောက်ဆုံး line က script ကို ဘယ်နည်းနဲ့ run နေလဲ ပြောပြပါတယ်။
<?php
declare(strict_types=1);
function welcome(string $name, int $lessonsLeft): string
{
return "Hi {$name}, {$lessonsLeft} lessons to go!";
}
$total = 36;
$done = 1;
echo welcome("Aye Aye", $total - $done), PHP_EOL;
echo "This script ran under the ", PHP_SAPI, " SAPI.", PHP_EOL;Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The opening tag
<?phpPHP က `<?php` နဲ့ `?>` ကြားက အပိုင်းကိုပဲ code အဖြစ် ယူပါတယ်။ File ရဲ့ အစမှာ ဒီ tag ရှိရင် အောက်က ကျန်တာ အားလုံးက PHP ပါ။
- Strict types
declare(strict_types=1);ဒီ line ကြောင့် `int` လိုချင်တဲ့ နေရာမှာ `"10"` လို string ပို့ရင် PHP က တိတ်တိတ်ပြောင်းမပေးတော့ဘဲ TypeError ပေးပါတယ်။ File ရဲ့ ပထမဆုံး statement ဖြစ်ရပါမယ်။
- Typed parameters and return type
function welcome(string $name, int $lessonsLeft): string`$name` က string၊ `$lessonsLeft` က int ဖြစ်ရမယ်၊ function က string တစ်ခု ပြန်ပေးရမယ် ဆိုတဲ့ ကတိသုံးခုကို line တစ်ကြောင်းတည်းမှာ ရေးထားပါတယ်။
- A string with variables inside
return "Hi {$name}, {$lessonsLeft} lessons to go!";Double quote string ထဲမှာ `{$name}` လို့ ရေးရင် variable ရဲ့ တန်ဖိုး ဝင်သွားပါတယ်။ ကွင်းကောက်ကြောင့် variable နာမည် ဘယ်မှာ ဆုံးလဲဆိုတာ ပြတ်ပြတ်သားသား သိရပါတယ်။
- Call and print
echo welcome("Aye Aye", $total - $done), PHP_EOL;`$total - $done` က int ဖြစ်လို့ ဒုတိယ parameter နဲ့ ကိုက်ပါတယ်။ `echo` မှာ comma နဲ့ ခြားပြီး တန်ဖိုးတွေ အများကြီး ထုတ်လို့ ရပါတယ်၊ `PHP_EOL` က line break ပါ။
- A built-in constant
echo "This script ran under the ", PHP_SAPI, " SAPI.", PHP_EOL;`PHP_SAPI` က PHP ကို ဘယ်လို စတင်ထားလဲ ပြောပြတဲ့ built-in constant ပါ။ Terminal ကနေ run ရင် `cli` ထွက်ပြီး web server အောက်ကနေ run ရင် တခြားတန်ဖိုး ထွက်ပါတယ်။
Hi Aye Aye, 35 lessons to go!
This script ran under the cli SAPI.- Browser က /welcome.php ကို တောင်းတယ်
- Web server က file ကို ရှာတယ်
- PHP engine က အပေါ်ကနေ အောက်ထိ run တယ်
- Output က HTTP response ဖြစ်လာတယ်
- Browser က HTML ကို ပြတယ်
File က .php ဖြစ်တယ်
PHP runs it and sends the output
File က .html ဖြစ်တယ်
Sent as it is, no PHP is executed
ဒီ course ကို ဘယ်လို ခွဲထားလဲ
Course မှာ lesson ၃၆ ခုကို အပိုင်းငါးပိုင်း ခွဲထားပြီး အပိုင်းတစ်ခုစီက အရင်အပိုင်းပေါ်မှာ အခြေခံပါတယ်။ အစီအစဉ်အတိုင်း သွားပါ။ Intermediate ထဲက form lesson တွေက function နဲ့ array ရေးတတ်ပြီးသားလို့ ယူဆထားပြီး database lesson တွေက form နဲ့ security အခြေခံကို သိပြီးသားလို့ ယူဆထားပါတယ်။
| အပိုင်း | Lesson | သင်ယူရမယ့်အရာ |
|---|---|---|
| Basic | ၁-၁၀ | Intro, setup, syntax, variable, echo နဲ့ print, string, number, array, operator |
| Intermediate | ၁၁-၁၉ | if/else, switch, loop, function, strict types, superglobal, GET နဲ့ POST, form, validation |
| Advanced | ၂၀-၃၂ | Date/time, include, namespace နဲ့ Composer, session, security, class, exception, MySQLi, PDO |
| Projects | ၃၃-၃၄ | Contact form project တစ်ခုနဲ့ အနှစ်ချုပ်၊ နောက်ထပ် သွားရမယ့် လမ်း |
| Exercises | ၃၅-၃၆ | အရင်အပိုင်းတွေကို ပြန်လေ့ကျင့်ဖို့ practice set နှစ်ခု |
Lesson တိုင်းက အစီအစဉ် အတူတူပါ: ရိုးရိုးရှင်းပြချက်၊ ပြဿနာ၊ အဓိက သဘောတရား၊ ယေဘုယျပုံစံ၊ line တစ်ကြောင်းချင်း ရှင်းပြထားတဲ့ ဥပမာ၊ အမှားများ၊ mini challenge နဲ့ quiz သုံးခုပါ။ ဥပမာတိုင်းကို ကိုယ်တိုင် run ပါ၊ တန်ဖိုးတစ်ခု ပြောင်းကြည့်ပါ၊ Enter မနှိပ်ခင် output ကို ကြိုမှန်းကြည့်ပါ။
ဥပမာတွေကို run ဖို့ နည်းလမ်းနှစ်မျိုး
အမြန်ဆုံးနည်းက terminal ကနေ တိုက်ရိုက် run တာပါ။ Form နဲ့ session လို browser လိုအပ်တဲ့ lesson တွေအတွက်တော့ PHP မှာ ပါပြီးသား development server ကို သုံးနိုင်ပါတယ်။ အဲဒီ server က လက်ရှိ folder ကို `localhost:8000` မှာ ဖွင့်ပေးပြီး လေ့လာဖို့အတွက်ပဲ ဖြစ်ပါတယ်၊ လူတွေ အသုံးပြုမယ့် website အတွက်တော့ မသုံးပါနဲ့။
php welcome.php # run one script and print its output in the terminal
php -S localhost:8000 # serve this folder to a browser (development only)
php -l welcome.php # check the syntax without running the scriptလက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ PHP
PHP က public web ရဲ့ အတော်များများကို အသုံးပြုနေတဲ့ language ပါ။ WordPress, Drupal လို content management system တွေကို PHP နဲ့ ရေးထားပြီး Laravel, Symfony လို framework တွေက shop, dashboard, API တွေကို စနစ်တကျ ဆောက်နိုင်အောင် ကူညီပါတယ်။ PHP သင်လိုက်တာဟာ ကုမ္ပဏီတွေ ဆက်ထိန်းပြီး တိုးချဲ့ဖို့ လူလိုနေတဲ့ code base အများကြီးရဲ့ language ကို သင်လိုက်တာလည်း ဖြစ်ပါတယ်။
ခေတ်သစ် PHP project တွေက ဟိုတုန်းက file တစ်ခုတည်း script တွေနဲ့ မတူတော့ပါဘူး။ Library တွေ install လုပ်ဖို့ Composer၊ test အတွက် PHPUnit၊ release မလုပ်ခင် type အမှားတွေ ရှာဖို့ PHPStan ဒါမှမဟုတ် Psalm တို့ကို သုံးပြီး PHP ကို Nginx ဒါမှမဟုတ် Apache နောက်ကွယ်မှာ အများအားဖြင့် PHP-FPM နဲ့ run ပါတယ်။ PHP 8.x မှာ type declaration, enum, attribute, match expression တွေ ပါလာတာကြောင့် ဒီ course က ပထမဆုံး ဥပမာကတည်းက `declare(strict_types=1)` ကို သင်ပေးတာပါ။
အမှားများ
အစပိုင်းမှာ မကြာခဏ ဖြစ်တတ်တဲ့ အမှားများ
၁။ Request မပို့ဘဲ file ကို တိုက်ရိုက်ဖွင့်တာ
`index.php` ကို double-click လုပ်တာ ဒါမှမဟုတ် address bar မှာ `C:/xampp/htdocs/index.php` လို path ရိုက်တာက browser ကို disk ပေါ်က file ကို တိုက်ရိုက် ဖတ်ခိုင်းတာပါ။ Server မပါလို့ PHP က မ run ဘဲ source code အကုန် ပေါ်လာတာ ဒါမှမဟုတ် download ဆွဲတာ ဖြစ်ပါတယ်။ `http://localhost:8000/index.php` လို server ကတစ်ဆင့် တောင်းပါ၊ ဒါမှမဟုတ် terminal မှာ `php index.php` နဲ့ run ပါ။
၂။ PHP က browser ထဲမှာ run တယ်လို့ ထင်တာ
PHP script က page ကို visitor ဆီ မရောက်ခင်မှာ အလုပ်ပြီးသွားပါပြီ။ Button ရဲ့ `onclick` က PHP function ကို မခေါ်နိုင်ပါဘူး၊ View Source မှာလည်း PHP ကို မမြင်ရပါဘူး၊ output ပဲ ပို့ထားလို့ပါ။ Click တစ်ခုကို တုံ့ပြန်ချင်ရင် link, form ဒါမှမဟုတ် JavaScript ရဲ့ `fetch` နဲ့ request အသစ် ပို့ရပြီး server ပေါ်မှာ PHP က နောက်တစ်ခါ run ပါတယ်။
၃။ ဖတ်ရုံပဲ ဖတ်ပြီး run မကြည့်တာ
PHP ရဲ့ warning တွေနဲ့ type error တွေကို ကိုယ်တိုင် မြင်ဖူးမှ အဓိပ္ပာယ် ရှိလာပါတယ်။ ဥပမာတိုင်းကို run ပြီး တမင် ဖျက်ကြည့်ပါ: semicolon တစ်ခု ဖယ်ကြည့်ပါ၊ int လိုချင်တဲ့နေရာမှာ string ထည့်ကြည့်ပြီး ပြန်လာတဲ့ message ကို ဖတ်ကြည့်ပါ။
Best Practices
ကောင်းတဲ့ အလေ့အကျင့်များ
- Lesson တစ်ခု ဒါမှမဟုတ် project တစ်ခုစီအတွက် folder တစ်ခုစီ ထားပါ၊ file path နဲ့ run command က ရိုးရှင်းနေပါလိမ့်မယ်။
- Script အသစ်တိုင်းကို `<?php` နဲ့ `declare(strict_types=1);` နဲ့ စပါ၊ type တွေကို ပထမနေ့ကတည်းက စစ်စေပါ။
- `php -l file.php` နဲ့ script ကို မ run ဘဲ syntax စစ်လို့ ရပါတယ်၊ save လုပ်တိုင်း စစ်ကြည့်ပါ။
Mini challenge
Mini challenge: course progress line
ပြီးသွားတဲ့ lesson အရေအတွက်နဲ့ စုစုပေါင်း lesson အရေအတွက်ကို လက်ခံပြီး `6 of 36 lessons done (17%)` လိုမျိုး string တစ်ခု ပြန်ပေးတဲ့ typed function `progressLine` ကို ရေးပါ။
- Parameter နှစ်ခုလုံးမှာ `int` type နဲ့ return မှာ `string` type ပေးပါ
- ရာခိုင်နှုန်းကို `round()` နဲ့ ကိန်းပြည့် လုပ်ပါ
- `$total` က 0 ဖြစ်ရင် `0%` ပြပါ (0 နဲ့ မစားပါနဲ့)
- 6/36 နဲ့ 0/0 နှစ်ခုလုံးအတွက် စာကြောင်းတစ်ကြောင်းစီ print ထုတ်ပါ
<?php
declare(strict_types=1);
// TODO: add int types to both parameters and a string return type
function progressLine($done, $total)
{
// TODO: percent = done / total * 100, rounded (use 0 when total is 0)
// TODO: return "<done> of <total> lessons done (<percent>%)"
}
// TODO: print the line for 6 of 36 and for 0 of 0ရလဒ်: `6 of 36 lessons done (17%)` နဲ့ `0 of 0 lessons done (0%)` ကို စာကြောင်းတစ်ကြောင်းစီ print ထုတ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
`$total === 0` ကို အရင်စစ်ပြီး ternary (`? :`) နဲ့ 0 ကို ရွေးပါ။ `round()` က float ပြန်ပေးလို့ `(int)` နဲ့ ကိန်းပြည့်ပြောင်းရင် `17` လို့ ပေါ်ပါမယ်။
အဖြေ ကြည့်ရန်
<?php
declare(strict_types=1);
function progressLine(int $done, int $total): string
{
$percent = $total === 0 ? 0 : (int) round($done / $total * 100);
return "{$done} of {$total} lessons done ({$percent}%)";
}
echo progressLine(6, 36), PHP_EOL;
echo progressLine(0, 0), PHP_EOL;Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- PHP က server ပေါ်မှာ run ပြီး browser ဆီ ရောက်တာက output (များသောအားဖြင့် HTML) ပဲ ဖြစ်ပါတယ်
- Request တစ်ခုချင်းစီအတွက် web server က `.php` file ကို PHP engine ဆီ လွှဲပေးပြီး print လုပ်တာတွေ response ဖြစ်လာပါတယ်
- `php script.php` နဲ့ web server မပါဘဲ terminal ကနေ ကိုယ်တိုင် run ကြည့်နိုင်ပါတယ်
- Course မှာ lesson ၃၆ ခုကို Basic, Intermediate, Advanced, Projects, Exercises အစီအစဉ်အတိုင်း သွားပါ
Migrating to PHP 8.5 — PHP Documentation Group