ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Loop ဆိုတာ "ဒါကို ထပ်လုပ်ပါ" လို့ ထပ်မရေးဘဲ ပြောတဲ့ နည်းပါ။ echo line တစ်ကြောင်းတည်းကို ဆယ်ခါ copy လုပ်မယ့်အစား တစ်ခါပဲ ရေးပြီး PHP ကို ဘယ်လောက်ထိ ထပ်လုပ်ရမလဲ ပြောလိုက်ရုံပါပဲ။ ကုန်ပစ္စည်း လေးဆယ်ကို ပြတဲ့ page၊ customer တစ်ယောက်ကို row တစ်ခုစီ ပါတဲ့ report၊ CSV file ရဲ့ line တိုင်းကို process လုပ်တဲ့ script တွေအားလုံးဟာ data အစုအဝေးတစ်ခုပေါ်က loop တွေပါ။
PHP မှာ loop statement လေးမျိုး ရှိပြီး ရွေးတဲ့အခါ ဘာကို ထပ်နေတာလဲဆိုတာပေါ် မူတည်ပါတယ်။ for က ရေတွက်တယ်၊ while က condition ပြောင်းသွားတဲ့အထိ စောင့်တယ်၊ do-while က အနည်းဆုံး တစ်ခါ run တယ်၊ foreach က array ရဲ့ element တွေကို တစ်ခုချင်း လျှောက်သွားတယ်။ နေ့စဉ် PHP ရေးရင်တော့ foreach ကို ကျန်တာတွေထက် အများကြီး ပိုသုံးရပါလိမ့်မယ်၊ ဘာလို့လဲဆိုတော့ web programming ရဲ့ အများစုက array တွေဖြစ်လို့ပါ — database က rows၊ form က fields၊ file က lines တွေပေါ့။
Problem → Why → Solution
ပြဿနာ: မရပ်တဲ့ loop နဲ့ တစ်ဆင့် ပိုပြေးတဲ့ loop
Loop တွေ အမှားမှာ တိတ်တိတ်လေး ပျက်ပါတယ်။ ပထမတစ်မျိုးက ဘယ်တော့မှ မပြီးတဲ့ loop — loop ထဲမှာ ဘာမှ condition ကို false ဆီ မရွှေ့လို့ အမြဲ true ဖြစ်နေတာပါ။ ဒုတိယတစ်မျိုးက တစ်ဆင့် ပိုပြေးတာ ဒါမှမဟုတ် တစ်ဆင့် လျော့ပြေးတာပါ။ အောက်က code မှာ နှစ်မျိုးလုံး ပါပါတယ်။ for loop က <= count() သုံးထားလို့ array ရဲ့ အဆုံးထက် element တစ်ခု ကျော်ဖတ်ပြီး while loop ကတော့ $n ကို မပြောင်းမိပါဘူး။
ပထမ loop ကို CLI မှာ run ရင် နာမည်သုံးခုကို print ထုတ်ပြီး Warning: Undefined array key 3 ထွက်ပါတယ်။ ဒုတိယ loop ကတော့ ဘယ်တော့မှ မရပ်ပါဘူး — CLI script မှာ time limit မရှိလို့ (အဲဒီမှာ max_execution_time က 0 ပါ) Ctrl+C နှိပ်တဲ့အထိ 5 ကို ထပ်ခါထပ်ခါ print ထုတ်နေပါလိမ့်မယ်။ Web request မှာတော့ PHP ရဲ့ default 30 စက္ကန့် limit က fatal error နဲ့ သတ်ပေးပေမယ့် server က မိနစ်ဝက်လောက် အလုပ်ရှုပ်နေပြီးမှ ဖြစ်ပါတယ်။
<?php
$items = ["pen", "ruler", "eraser"];
// 1. Off by one: valid indexes are 0, 1, 2, but <= lets $i reach 3
for ($i = 0; $i <= count($items); $i++) {
echo $items[$i] . "\n";
}
// 2. Nothing changes $n, so the condition is true forever
$n = 5;
while ($n > 0) {
echo $n . "\n";
}ဘာကြောင့် PHP က ဒါတွေကို မတားတာလဲ
PHP က ကိုယ်ရည်ရွယ်တာကို မသိပါဘူး။ $i <= count($items) ဆိုတာ ရေးထုံးအရ မှန်တဲ့ condition ပါ၊ update မပါတဲ့ loop လည်း ရေးထုံးအရ မှန်ပါတယ်။ Syntax error မဟုတ်လို့ ပြဿနာကို runtime မှာ warning အနေနဲ့ ဒါမှမဟုတ် ရပ်သွားတဲ့ page အနေနဲ့ပဲ ပထမဆုံး တွေ့ရပါမယ်။ ကုစားနည်းက loop မရေးခင် pass တိုင်းမှာ ဘာပြောင်းလဲမလဲ၊ ဘယ် value ရောက်ရင် ရပ်မလဲဆိုတာ ကြိုဆုံးဖြတ်ထားဖို့ပါ။ Array တွေအတွက်တော့ foreach ကို ရွေးပါ၊ ဒါဆို အမှားမှားနိုင်တဲ့ index မရှိတော့ပါဘူး။
Core concept: loop တိုင်းမှာ start, stop, step ရှိရမယ်
Loop တိုင်းက မေးခွန်းသုံးခုကို ဖြေရပါတယ် — ဘယ်ကစမလဲ၊ ဘယ်အချိန်ရပ်မလဲ၊ pass တစ်ခုနဲ့တစ်ခုကြား ဘာပြောင်းလဲမလဲ။ for loop က အဲဒီသုံးခုလုံးကို header ထဲမှာ ထည့်ပေးတာမို့ for ($i = 0; $i < 10; $i++) က ရေတွက်ဖို့ သင့်တော်ပါတယ်။ while loop ကတော့ header ထဲမှာ condition ပဲ ရှိလို့ body က condition ကြည့်နေတဲ့ ဘာတစ်ခုကို ပြောင်းပေးဖို့ ကိုယ်တိုင် သေချာစေရပါမယ်။ do-while လည်း အတူတူပါပဲ၊ ဒါပေမယ့် condition ကို body ပြီးမှ စစ်လို့ body က အနည်းဆုံး တစ်ခါ run ပါတယ်။ Loop ကို ဘာက အဆုံးဆီ တွန်းပို့လဲ မပြောနိုင်ရင် run မကြည့်ခင်ကတည်းက bug ကို တွေ့ပြီးသားပါ။
| Loop | ဘယ်အချိန် သုံးမလဲ |
|---|---|
| for | အကြိမ်အရေအတွက် သိတယ်၊ ဒါမှမဟုတ် counter ကို ကိုယ်တိုင် လိုတယ် |
| while | တစ်ခုခု ပြောင်းသွားတဲ့အထိ ထပ်လုပ်ချင်တယ် (ဥပမာ file ကို အဆုံးထိ ဖတ်တာ) |
| do-while | ပထမအကြိမ်ကို condition မစစ်ခင် မဖြစ်မနေ run ရမယ် (ဥပမာ retry) |
| foreach | array (ဒါမှမဟုတ် iterable) ရှိပြီး element တိုင်းကို လိုတယ် |
foreach ကို အာရုံအစိုက်ဆုံး ထားသင့်ပါတယ်၊ ဘာလို့လဲဆိုတော့ အများဆုံး ရေးရမယ့် loop မို့ပါ။ foreach ($prices as $price) က element တစ်ခုချင်းစီရဲ့ value ကို ပေးပြီး foreach ($prices as $name => $price) က key ပါ ပေးပါတယ်။ Default အနေနဲ့ loop variable က element ရဲ့ copy ဖြစ်လို့ ဒီ variable ကို assign လုပ်ရင် array မှာ ဘာမှ မပြောင်းပါဘူး။ PHP က array ရဲ့ snapshot ပေါ်မှာ လှည့်လို့ loop ထဲက array ကို append လုပ်ရင်လည်း loop မရှည်သွားပါဘူး — element သုံးခုပါတဲ့ array ကို loop ထဲမှာ element တစ်ခု ထပ်ထည့်ရင် loop က သုံးခါပဲ run ပြီး array ကတော့ element လေးခု ဖြစ်သွားပါတယ်။
PHP အထူးသဖြင့် beginner တွေ မှားတတ်တဲ့ အချက်နှစ်ခု ရှိပါတယ်။ ပထမတစ်ခုက PHP မှာ block scope မရှိတာပါ — loop header ဒါမှမဟုတ် body ထဲက $i, $price လို variable တွေဟာ သာမန် variable တွေဖြစ်လို့ loop ပြီးသွားပါတယ်ဆိုရင်တောင် နောက်ဆုံး ရထားတဲ့ value နဲ့ ဆက်ရှိနေပါတယ်။ ဒုတိယတစ်ခုက အဲဒီ ဆက်ရှိနေတာက reference နဲ့ဆိုရင် အန္တရာယ်ရှိလာတာပါ — ပထမ deeper section မှာ ကြည့်ပါမယ်၊ by-reference foreach က unset() မလုပ်မချင်း loop variable ကို နောက်ဆုံး element နဲ့ ချိတ်ထားခဲ့ပါတယ်။ ထပ်ဖြည့်ရမှာက control statement တွေပါ — break က loop ကထွက်၊ continue က နောက် pass ကို ကျော်သွား၊ နှစ်ခုလုံးမှာ break 2 လို number ပေးပြီး nested loop အလွှာများစွာကို တစ်ပြိုင်နက် ထွက်လို့ ရပါတယ်။
- Start value ထား ဒါမှမဟုတ် array ရွေး
- Pass မတိုင်ခင် condition စစ်
- ဒီ pass အတွက် body ကို run
- တစ်ဆင့်တိုး ပြီးမှ ထပ်စစ်
foreach မှာ step ကို PHP က ကိုယ်စား လုပ်ပေးပါတယ်။ while မှာတော့ step ကို ကိုယ်တိုင် ရေးရလို့ မေ့တာက infinite loop ဖြစ်ပါတယ်။
အထွေထွေ ပုံစံ
for (init; condition; step) { ... }
while (condition) { ... }
do { ... } while (condition);
foreach ($collection as $key => $value) { ... }
break; // leave this loop (break 2 leaves two levels)
continue; // jump to the next pass (continue 2 for the outer loop)for ရဲ့ header မှာ semicolon နှစ်ခုနဲ့ ခွဲထားတဲ့ အပိုင်းသုံးပိုင်း ပါပြီး while နဲ့ do-while မှာ condition တစ်ခုတည်း ပါပါတယ်။ foreach မှာ $key => ကို မလိုရင် ထည့်စရာ မလိုပါဘူး။ break နဲ့ continue ကို number ပေးရင် အပြင်ဘက် loop အလွှာတွေအထိ သက်ရောက်ပါတယ်။
Example: counter, ကုန်သွားတဲ့အထိ နှုတ်တာနဲ့ ဈေးနှုန်းစာရင်း
ဒီ script က loop သုံးမျိုးကို သေးသေးလေး လက်တွေ့ဆန်တဲ့ data နဲ့ သုံးပြပါတယ်။ CLI မှာ \n တစ်ခုစီက row တစ်ကြောင်းစီ ထွက်ပါတယ်။ Browser ထဲမှာဆိုရင်တော့ browser က ရိုးရိုး newline ကို လျစ်လျူရှုလို့ <br> ထည့်ဒါမှမဟုတ် HTML element ထဲ ထည့်ရပါမယ်။
<?php
// for: a counter you control
for ($day = 1; $day <= 3; $day++) {
echo "Day $day\n";
}
// while: repeat until a condition changes
$stock = 12;
$weeks = 0;
while ($stock > 0) {
$stock -= 4;
$weeks++;
}
echo "Stock sold out after $weeks weeks\n";
// foreach: read every item together with its key
$prices = ["Laptop" => 1200, "Mouse" => 25, "Keyboard" => 70];
foreach ($prices as $name => $price) {
echo str_pad($name, 10) . number_format($price) . " USD\n";
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- for header
for ($day = 1; $day <= 3; $day++) {Start ($day = 1)၊ stop condition ($day <= 3)၊ step ($day++) သုံးခုလုံး တစ်ကြောင်းတည်းမှာ ရှိပါတယ်။ $day က 4 ရောက်တဲ့အခါ condition false ဖြစ်ပြီး loop ရပ်ပါတယ်။
- while condition
while ($stock > 0) {စာရင်းထဲမှာ ပစ္စည်း ကျန်သေးသရွေ့ ထပ်လုပ်ပါတယ်။ ဒီ loop ရဲ့ header မှာ condition ပဲ ပါလို့ stop ကို body ထဲက ပြောင်းလဲမှုက ဆုံးဖြတ်ပါတယ်။
- the step
$stock -= 4;ဒါက while loop ရဲ့ step ပါ။ တစ်ပတ်ကို 4 ခုရောင်းတယ်ဆိုပြီး 12 ကနေ 0 ထိ သုံးပတ်မှာ ရောက်ပါတယ်။ ဒီ line ကို ဖယ်လိုက်ရင် $stock က 12 အတိုင်း ကျန်ပြီး loop မရပ်တော့ပါဘူး။
- foreach with key
foreach ($prices as $name => $price) {Associative array ရဲ့ item တစ်ခုစီအတွက် key ကို $name ထဲ၊ value ကို $price ထဲ ထည့်ပေးပါတယ်။ Counter မရှိလို့ index မှားစရာ မရှိပါဘူး။
- formatting the row
echo str_pad($name, 10) . number_format($price) . " USD\n";str_pad() က နာမည်ကို 10 လုံးအထိ space ဖြည့်ပေးလို့ ဈေးနှုန်းတွေ တစ်တန်းတည်း ညီပါတယ်။ number_format() က 1200 ကို 1,200 လို့ ပြောင်းပေးပါတယ်။
Day 1
Day 2
Day 3
Stock sold out after 3 weeks
Laptop 1,200 USD
Mouse 25 USD
Keyboard 70 USDforeach by value နဲ့ by reference
foreach ($prices as $price) မှာ $price က copy ပါ၊ ဒါကြောင့် array ထဲက ဈေးတွေကို ပြင်ချင်ရင် $price ကို assign လုပ်တာနဲ့ မရပါဘူး။ foreach ($prices as &$price) လို့ & ထည့်ရင် $price က element ရဲ့ alias ဖြစ်သွားပြီး assign လုပ်တာတိုင်း array ထဲကို တိုက်ရိုက် ရောက်ပါတယ်။ ပြဿနာက loop ပြီးတဲ့အခါ $price က နောက်ဆုံး element ($prices[2]) ကို ဆက်ညွှန်နေတာပါ။ ဒီအတိုင်း ထားပြီး ပုံမှန် foreach ($prices as $price) ကို ထပ်သုံးရင် loop တစ်ပတ်တိုင်းက $prices[2] ကို ကိုယ့်ဘာသာ overwrite လုပ်သွားပါတယ်။
<?php
$prices = [10, 20, 30];
// by value: $price is a copy, so the array stays the same
foreach ($prices as $price) {
$price = $price * 2;
}
echo implode(", ", $prices), "\n";
// by reference: & makes $price an alias of each element
foreach ($prices as &$price) {
$price = $price * 2;
}
echo implode(", ", $prices), "\n";
// $price is STILL a reference to $prices[2] here
foreach ($prices as $price) {
}
echo implode(", ", $prices), "\n";
unset($price); // break the link after any by-reference loopOutput က 10, 20, 30 ၊ 20, 40, 60 ၊ ပြီးတော့ 20, 40, 40 ပါ။ နောက်ဆုံး loop မှာ $prices[2] ထဲကို 20၊ 40၊ ပြီးတော့ 40 (အခု ရှိနေတဲ့ နောက်ဆုံး value) အစဉ်လိုက် ရေးသွားလို့ 60 ပျောက်သွားတာပါ။ ကာကွယ်နည်းက by-reference loop တိုင်းရဲ့ ပြီးနောက် unset($price) ရေးတာပါ။ ဒါမှမဟုတ် reference ကို လုံးဝ ရှောင်ပြီး foreach ($prices as $i => $p) { $prices[$i] = $p * 2; } လို key နဲ့ ပြန်ရေးတာ၊ array_map() သုံးတာတွေ လုပ်နိုင်ပါတယ်။
break, continue နဲ့ do-while
break က လက်ရှိ loop ကို ချက်ချင်း ရပ်ပြီး continue က လက်ရှိ pass ကို ကျော်ပြီး နောက် pass ကို သွားပါတယ်။ Loop ထဲမှာ loop ရှိနေတဲ့အခါ number ပါတဲ့ ပုံစံက အသုံးဝင်ပါတယ်။ continue 2 က အပြင်ဘက် loop ရဲ့ နောက် pass ကို သွားပြီး break 2 က loop နှစ်ခုလုံးကို ထွက်ပါတယ်။ အောက်က script မှာ order တစ်ခုထဲမှာ quantity 0 တစ်ကြောင်း တွေ့တာနဲ့ order တစ်ခုလုံးကို ကျော်ပြီး ကားပြည့်တဲ့အခါ တစ်ခုလုံးကို ရပ်ပါတယ်။
<?php
$orders = [
["id" => 101, "lines" => [2, 1, 4]],
["id" => 102, "lines" => [3, 0, 5]],
["id" => 103, "lines" => [1, 1]],
["id" => 104, "lines" => [6]],
];
$shipped = [];
foreach ($orders as $order) {
foreach ($order["lines"] as $qty) {
if ($qty === 0) {
echo "Order {$order['id']} skipped: zero quantity\n";
continue 2; // next ORDER, not the next line
}
}
$shipped[] = $order["id"];
if (count($shipped) === 2) {
break; // the truck is full
}
}
echo "Shipped: " . implode(", ", $shipped) . "\n";
$attempt = 10;
do {
echo "do-while runs once even though 10 < 3 is false\n";
} while ($attempt < 3);Order 102 ကို ကျော်ပြီး 101 နဲ့ 103 ကို ပို့ကာ ကားပြည့်လို့ order 104 ကို မကြည့်တော့ပါဘူး။ နောက်ဆုံး do-while ကတော့ $attempt < 3 က ပထမကတည်းက false ဖြစ်ပေမယ့် body ကို တစ်ခါ run ပါတယ်၊ ဒါကြောင့် "အနည်းဆုံး တစ်ခါ လုပ်ရမယ်" ဆိုတဲ့ retry, menu loop တွေမှာ သုံးပါတယ်။ Number ပေးတဲ့ break/continue ကို အလွှာ နှစ်ထပ်ထက် ပိုမသုံးပါနဲ့ — လိုလာရင် အတွင်းက loop ကို function အဖြစ် ခွဲပြီး return လုပ်တာက ဖတ်ရလွယ်ပါတယ်။
list() နဲ့ [ ] ကို foreach ထဲမှာ သုံးပြီး unpack လုပ်ခြင်း
Row တစ်ခုက array ဖြစ်နေရင် $row[0], $row[1] လို index တွေနဲ့ ဖတ်တာထက် foreach ရဲ့ header ထဲမှာပဲ ခွဲထုတ်လို့ ရပါတယ်။ [$id, $name] ဆိုတဲ့ ပုံစံက position အလိုက် ယူပြီး ["id" => $id] ဆိုတဲ့ ပုံစံက key နာမည်အလိုက် ယူပါတယ်။ [$first, , $third] လို့ comma ကို အလွတ်ထားရင် အဲဒီ position ကို ကျော်ပါတယ်။ အဟောင်း list($a, $b) = ... ပုံစံနဲ့ အတူတူပါပဲ။ ရှိမနေတဲ့ index/key ကို တောင်းရင် Warning: Undefined array key နဲ့ null ရပါတယ်။
<?php
$rows = [
[1, "Aye", 85],
[2, "Min", 72],
];
foreach ($rows as [$id, $name, $score]) {
echo "#$id $name scored $score\n";
}
$users = [
["id" => 7, "email" => "aye@example.com"],
["id" => 9, "email" => "min@example.com"],
];
foreach ($users as ["id" => $id, "email" => $email]) {
echo "$id: $email\n";
}
[$first, , $third] = ["a", "b", "c"];
echo $first . $third . "\n";
$a = 1;
$b = 2;
[$a, $b] = [$b, $a]; // swap without a temp variable
echo "$a $b\n";ဒီ code က #1 Aye scored 85၊ #2 Min scored 72၊ ပြီးတော့ 7: aye@example.com နဲ့ 9: min@example.com၊ ac ၊ 2 1 လို့ ထုတ်ပါတယ်။ Database ရလဒ်လို associative row တွေမှာ key နဲ့ unpack လုပ်ရင် column ရဲ့ အစီအစဉ် ပြောင်းသွားလည်း code မပျက်ပါဘူး။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
PHP page တစ်ခုရဲ့ ဝက်လောက်က loop တွေပါ။ Product list၊ comment thread၊ admin table ရဲ့ row တစ်ခုချင်းစီဟာ database ကနေ ရလာတဲ့ array ပေါ်က foreach တွေပါ။ HTML template ထဲမှာတော့ foreach (...): နဲ့ endforeach; ဆိုတဲ့ alternative syntax ကို သုံးလေ့ရှိပါတယ်၊ ဒါမှ အပိတ်ကွင်း } တွေက HTML ကြားမှာ ပျောက်မသွားပါဘူး။ ပြတဲ့ value တိုင်းကို htmlspecialchars() နဲ့ escape လုပ်ရပါမယ်။
<?php $products = ["Laptop", "Mouse <b>"]; ?>
<ul>
<?php foreach ($products as $product): ?>
<li><?= htmlspecialchars($product, ENT_QUOTES, 'UTF-8') ?></li>
<?php endforeach; ?>
</ul>Page ပြဆိုင်ရာ မဟုတ်တဲ့ နေရာတွေမှာလည်း loop တွေ ရှိပါတယ်။ ကြီးတဲ့ file ကို while (($line = fgets($handle)) !== false) နဲ့ တစ်လိုင်းချင်း ဖတ်ရင် memory တစ်ခုလုံး မသုံးရပါဘူး။ API ကို ခေါ်တဲ့အခါ retry loop က အများဆုံး ကြိုးစားမယ့် အကြိမ်အရေအတွက်နဲ့ break ကို တွဲသုံးပါတယ်။ Pagination loop၊ cron job နဲ့ batch import တွေလည်း အတူတူပါပဲ။ Production code မှာ loop တိုင်းမှာ "ဘာက ရပ်စေလဲ" ကို အမြဲ မေးပါ — unbounded while က request တစ်ခုက server ကို မိနစ်ချီ ချုပ်ထားစေနိုင်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. By-reference loop ပြီးနောက် unset() မေ့တာ
foreach ($rows as &$row) ပြီးနောက် $row က နောက်ဆုံး element ကို ညွှန်ဆဲပါ။ နောက်ပိုင်း code က $row ကို variable အသစ်လို ထပ်သုံး (foreach ($other as $row) လိုမျိုး) လိုက်ရင် နောက်ဆုံး row ကို တိတ်တိတ်လေး ပြောင်းသွားပါတယ်။ Warning တစ်ခုမှ မထွက်လို့ ရှာရခက်ပါတယ်။ Loop ပြီးတာနဲ့ unset($row); ရေးပါ။
2. Copy ကို ပြင်ပြီး array ပြောင်းမယ်လို့ မျှော်လင့်တာ
foreach ($prices as $price) { $price *= 2; } က $prices ကို ဘာမှ မပြောင်းပါဘူး၊ $price က copy မို့ပါ။ Array ကို ပြောင်းချင်ရင် key နဲ့ ပြန်ရေးပါ ($prices[$i] = ...)၊ ဒါမှမဟုတ် array_map() သုံးပါ၊ reference လိုပြီဆိုမှ & ကို unset() နဲ့ တွဲပြီး သုံးပါ။
3. <= count() နဲ့ off-by-one ဖြစ်တာ
Array တစ်ခုရဲ့ index တွေက 0 ကစပြီး count() - 1 မှာ ဆုံးပါတယ်။ $i <= count($items) လို့ ရေးရင် နောက်ဆုံး pass က မရှိတဲ့ index ကို ဖတ်ပြီး Warning: Undefined array key ထွက်ပါတယ်။ $i < count($items) လို့ ရေးပါ၊ ဒါမှမဟုတ် foreach ကို သုံးပါ။
4. Array မဟုတ်တဲ့ value ကို foreach လုပ်တာ
Function တစ်ခုက အလုပ်မအောင်ရင် array အစား false ဒါမှမဟုတ် null ပြန်တတ်ပါတယ်။ အဲဒါကို တိုက်ရိုက် foreach လုပ်ရင် Warning: foreach() argument must be of type array|object, null given ထွက်ပါတယ်။ Loop မလုပ်ခင် ပြန်လာတဲ့ value ကို စစ်ပါ၊ ဒါမှမဟုတ် ?? [] နဲ့ array အလွတ်ကို fallback ပေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Array ကို လျှောက်ဖတ်ရင် foreach ကို ပထမဆုံး ရွေးပါ၊ for + index ကို index ကို တကယ်လိုမှ သုံးပါ။
- while loop ရေးတိုင်း "ဘာက condition ကို false ဖြစ်စေလဲ" ကို ကိုယ့်ကိုယ်ကို မေးပါ၊ retry တွေမှာ အများဆုံး ကြိုးစားမယ့် အကြိမ်အရေအတွက်ကို ထည့်ပါ။
- foreach (... as &$x) သုံးရင် loop ပြီးတာနဲ့ unset($x); ကို ချက်ချင်း ရေးပါ။
- Loop အတွင်းက array ကို အကြီးအကျယ် ပြင်မယ့်အစား ရလဒ်ကို array အသစ်ထဲ စုပြီး ပြီးမှ ပြောင်းပါ။
Mini challenge
Mini challenge: ရုပ်ရှင်ရုံ ထိုင်ခုံ စစ်ဆေးခြင်း
ရုပ်ရှင်ရုံ ထိုင်ခုံ map (row တစ်ခုစီမှာ ရောင်းပြီးရင် true၊ လွတ်ရင် false) ကို စစ်တဲ့ function နှစ်ခု ရေးပါ။ ပထမ ရှိတဲ့ လွတ်တဲ့ ထိုင်ခုံကို ရှာပြီး ရောင်းပြီးသား ထိုင်ခုံ အရေအတွက်ကို ရေတွက်ပါ။
- firstFreeSeat(array $seats): ?string — "B2" လို row စာလုံး + ထိုင်ခုံနံပါတ် (1 က စ) ပြန်ပေးရမယ်၊ မလွတ်ရင် null
- ထိုင်ခုံ တွေ့တာနဲ့ nested foreach နှစ်ခုလုံးကို break 2 နဲ့ ထွက်ရမယ် (loop ထဲမှာ return မသုံးရ)
- countTaken(array $seats): int — လွတ်တဲ့ ထိုင်ခုံတွေကို continue နဲ့ ကျော်ပြီး ရောင်းပြီးတာကို ရေတွက်ရမယ်
- Row အားလုံး ရောင်းပြီးတဲ့ hall တစ်ခုအတွက် "sold out" လို့ print ထုတ်ရမယ်
<?php
function firstFreeSeat(array $seats): ?string
{
// TODO: foreach over rows (row letter => cells), then over the cells
// TODO: a free seat is false; build the label and leave BOTH loops
return null;
}
function countTaken(array $seats): int
{
// TODO: count only the true values, using continue for free seats
return 0;
}
// TODO: build a 3-row seat map, then print the first free seat and the sold count
// TODO: test a hall that is completely sold outရလဒ်: First free seat: B2 Taken seats: 6 Full hall: sold out
အရိပ်အမြွက် ကြည့်ရန်
Row နာမည်က array key ဖြစ်လို့ foreach ($seats as $row => $cells) လို့ ရေးပါ။ ထိုင်ခုံနံပါတ်က index + 1 ပါ။ $found ကို null နဲ့ စပြီး break 2 နဲ့ ထွက်ပြီးမှ $found ကို return လုပ်ပါ။ Null ကို ?? နဲ့ "sold out" ပြောင်းပြီး print ထုတ်နိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
<?php
function firstFreeSeat(array $seats): ?string
{
$found = null;
foreach ($seats as $row => $cells) {
foreach ($cells as $index => $taken) {
if (!$taken) {
$found = $row . ($index + 1);
break 2;
}
}
}
return $found;
}
function countTaken(array $seats): int
{
$count = 0;
foreach ($seats as $cells) {
foreach ($cells as $taken) {
if (!$taken) {
continue;
}
$count++;
}
}
return $count;
}
$seats = [
"A" => [true, true, true],
"B" => [true, false, true],
"C" => [false, false, true],
];
echo "First free seat: " . (firstFreeSeat($seats) ?? "sold out") . "\n";
echo "Taken seats: " . countTaken($seats) . "\n";
$full = ["A" => [true, true]];
echo "Full hall: " . (firstFreeSeat($full) ?? "sold out") . "\n";Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Loop တိုင်းမှာ start, stop, step သုံးချက်ရှိရမယ်၊ ဘာက loop ကို ရပ်စေလဲ မပြောနိုင်ရင် ရေးမစပါနဲ့။
- Array အတွက် foreach၊ ရေတွက်ဖို့ for၊ ပြောင်းလဲမှုကို စောင့်ဖို့ while၊ အနည်းဆုံး တစ်ခါ run ဖို့ do-while ကို ရွေးပါ။
- foreach by value က copy ကို ပြင်တာဖြစ်လို့ array မပြောင်းပါဘူး၊ by reference (&) ကို သုံးရင် loop ပြီးတာနဲ့ unset() လုပ်ပါ။
- break N / continue N က nested loop ကို ထိန်းပြီး [$a, $b] နဲ့ list() က row တွေကို header ထဲမှာပဲ unpack လုပ်ပေးပါတယ်။
PHP Language Reference — PHP Documentation Group