Thuta Learning
PHP
IntermediateProgrammingအခြေခံ

Functions

PHPသင်ခန်းစာ 1430 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Parameter type, return type, ?int လို nullable နဲ့ int|float လို union type ပါတဲ့ function ကို ရေးနိုင်ဖို့
  • echo နဲ့ return ကို ခွဲခြားပြီး ရလဒ်ကို caller က ပြန်သုံးလို့ ရအောင် ရေးနိုင်ဖို့
  • Default value, named argument နဲ့ variadic (...$items) parameter တွေကို သုံးနိုင်ဖို့
  • Function scope ကို နားလည်ပြီး closure (use), arrow function fn နဲ့ static variable ကို ရွေးသုံးနိုင်ဖို့
  • Argument ကို by value နဲ့ by reference (&) ပို့တာ ဘာကွာလဲ ရှင်းပြနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Function ဆိုတာ နာမည်ပေးထားတဲ့ code အစုတစ်ခုပါ — တစ်ခါရေးပြီး နေရာများစွာကနေ ခေါ်သုံးလို့ ရပါတယ်။ ဈေးနှုန်းတွက်တာ၊ စာသား format လုပ်တာ၊ input စစ်တာ၊ database helper တွေလို ထပ်ခါထပ်ခါ ပါလာတဲ့ logic တွေကို function ထဲထည့်ထားရင် code က သန့်ပြီး ပြင်ရလွယ်ပါတယ်။

Function ကို စက်ငယ်လေးတစ်လုံးလို မြင်ကြည့်ပါ။ Argument တွေကို အဝင်ပေါက်ကနေ ထည့်လိုက်ရင် စက်က ကိုယ့်ဘာသာ အလုပ်လုပ်ပြီး return value တစ်ခုကို အထွက်ပေါက်ကနေ ပြန်ပေးပါတယ်။ ကောင်းတဲ့ function ရဲ့ အလုပ်က တစ်ခုတည်းပါ၊ ပြီးတော့ ရလဒ်ကို print ထုတ်ပစ်မယ့်အစား ပြန်ပေးတာမို့ ခေါ်တဲ့သူက ဘာလုပ်မလဲ ကိုယ်တိုင် ရွေးလို့ ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: ရလဒ်က echo ထဲမှာပဲ ကျန်ခဲ့တယ်၊ variable ကလည်း မမြင်ဘူး

Function အသစ် ရေးတဲ့သူ အများစုက ဒီအမှားနှစ်ခုထဲက တစ်ခုကို တွေ့ပါတယ်။ ပထမတစ်ခုက ရလဒ်ကို return မလုပ်ဘဲ echo လုပ်လိုက်တာပါ။ Screen မှာ 1000 ပေါ်လို့ အလုပ်လုပ်သလို ထင်ရပေမယ့် variable ထဲမှာတော့ null ပဲ ရှိနေလို့ နောက်တွက်ချက်မှုတွေ အားလုံး မှားသွားပါတယ်။ ဒုတိယတစ်ခုက function အပြင်က variable ကို function ထဲက မြင်မယ်လို့ ထင်တာပါ၊ PHP က အဲဒီလို မမြင်ပါဘူး။

အောက်က code ကို run ကြည့်ရင် 1000 ကို print ထုတ်ပြီး Total with shipping: 500 လို့ ထွက်ပါတယ် (1500 မဟုတ်ပါဘူး)၊ ပြီးတော့ addTax() က Warning: Undefined variable $taxRate ပေးပြီး tax မပါဘဲ 100 ပြန်ပေးပါတယ်။

broken-functions.php
<?php
function calculateTotal($price, $qty) {
    echo $price * $qty;            // prints, but returns nothing
}
$total = calculateTotal(250, 4);   // $total is null
$withShipping = $total + 500;      // 500, not 1500
echo "\nTotal with shipping: $withShipping\n";

$taxRate = 0.05;
function addTax($price) {
    return $price * (1 + $taxRate); // $taxRate does not exist in here
}
echo addTax(100), "\n";

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

echo က ရလဒ်ကို output ဆီ ပို့လိုက်ရုံပါ၊ ရလဒ်ကို caller ဆီ ပြန်မပေးပါဘူး။ Return မပါတဲ့ function က null ပြန်ပါတယ်။ ဒါ့အပြင် PHP function တိုင်းမှာ ကိုယ်ပိုင် local scope ရှိပြီး အပြင်က variable တွေကို အလိုအလျောက် မမြင်ရပါဘူး၊ ဒါကြောင့် $taxRate ကို တန်ဖိုးမရှိတဲ့ variable အဖြစ် ဆက်ဆံတာပါ။ ကုစားနည်းက return နဲ့ ရလဒ်ပြန်ပေးပြီး လိုတဲ့ တန်ဖိုးကို parameter အဖြစ် ပို့ပေးတာပါ။

Core concept: function က ကိုယ်ပိုင် scope ရှိတဲ့ ကတိတစ်ခုပါ

Function ကို function name(parameters): returnType { ... } လို့ ကြေညာပြီး name(arguments) လို့ ခေါ်ပါတယ်။ Parameter တွေက function ထဲမှာ local variable တွေ ဖြစ်လာပြီး return statement က value တစ်ခုကို caller ဆီ ပြန်ပို့ကာ function ကို ချက်ချင်း ပြီးဆုံးစေပါတယ်။ Function ထဲမှာ ဖန်တီးတဲ့ variable တိုင်းက function ပြီးရင် ပျောက်သွားပြီး အပြင်က variable တွေကို မမြင်ရပါဘူး။ တန်ဖိုးကို အထဲ ယူသွင်းဖို့ နည်းသုံးမျိုးရှိပါတယ် — parameter (အကောင်းဆုံး)၊ closure ရဲ့ use ၊ ပြီးတော့ global keyword (ရှောင်သင့်တယ်)။

Type declaration တွေက function ကို ကတိတစ်ခု ဖြစ်စေပါတယ်။ int $qty ဆိုတာ "integer တစ်ခု ပေးပါ" လို့ ပြောတာဖြစ်ပြီး : float ဆိုတာ "float တစ်ခု ပြန်ပေးမယ်" လို့ ကတိပေးတာပါ။ ?string က string ဒါမှမဟုတ် null၊ int|float လို union type က ဖော်ပြတဲ့ type တစ်ခုခု၊ void ကတော့ အသုံးဝင်တဲ့ value မပြန်ဘူးလို့ ဆိုလိုပါတယ်။ Type မှားရင် PHP က TypeError throw လုပ်ပါတယ်။ ဒါပေမယ့် default အနေနဲ့ "5" လို numeric string ကို int ဖြစ်အောင် အလိုအလျောက် ပြောင်းပေးတာတွေ ရှိလို့ declare(strict_types=1) ရဲ့ အခန်းမှာ အသေးစိတ် ကြည့်ပါမယ်။

ပုံစံဥပမာအဓိပ္ပာယ်
Typedint $qtyပို့လာတဲ့ value က int ဖြစ်ရမယ်
Defaultstring $role = "member"မပို့ရင် default ကို သုံးမယ်
Nullable?int $limitint ဒါမှမဟုတ် null ပို့လို့ ရတယ်
Variadicint ...$numbersကျန်တဲ့ argument အားလုံးကို array တစ်ခု အဖြစ် စုပေးတယ်
By referencearray &$listcaller ရဲ့ variable ကို တိုက်ရိုက် ပြင်မယ်

Argument တွေကို default အနေနဲ့ by value ပို့ပါတယ်၊ ဒါကြောင့် function ထဲမှာ parameter ကို ပြောင်းလည်း caller ရဲ့ variable ကို မထိပါဘူး (array ကိုတောင် copy လုပ်ပါတယ်)။ Parameter ရှေ့မှာ & ထည့်မှ caller ရဲ့ variable ကို တိုက်ရိုက် ပြောင်းပါတယ်။ PHP 8 ကစပြီး argument ကို name နဲ့ ပို့လို့ရလို့ createUser(active: false) လို optional parameter တစ်ခုကို ကျော်ပြီး နောက်တစ်ခုကို သတ်မှတ်လို့ရပါတယ်၊ ပြီးတော့ ...$items နဲ့ argument အရေအတွက် မသတ်မှတ်ဘဲ လက်ခံလို့ ရပါတယ်။

မှတ်ထားရမယ့် ပုံစံ: ကတိ + ကိုယ်ပိုင်အခန်း
  1. Caller က argument တွေကို ပို့
  2. PHP က စစ်ပြီး local variable ထဲ copy
  3. Body က ကိုယ်ပိုင် scope ထဲမှာ run
  4. return က value တစ်ခုကို ပြန်ပို့

Function အပြင်က တစ်ခုခု လိုရင် parameter နဲ့ ပို့ပါ၊ အပြင်ကို ရလဒ်ပေးချင်ရင် return နဲ့ ပြန်ပေးပါ။

အထွေထွေ ပုံစံ

php
function name(Type $required, Type $optional = default, Type ...$rest): ReturnType
{
    // local variables live only inside here
    return $value;
}

$closure = function (Type $x) use ($outer): ReturnType { return $value; };
$arrow = fn(Type $x): ReturnType => expression;

name(optional: value, required: value);   // named arguments

Required parameter တွေ အရင်လာပြီး default ရှိတဲ့ parameter တွေ နောက်ကလာပါတယ်၊ variadic ကတော့ အဆုံးမှာပါ။ Closure က use နဲ့ အပြင် variable ကို တောင်းယူပြီး arrow function က အပြင် variable တွေကို အလိုအလျောက် (by value) ယူပါတယ်။ Named argument ကို ပို့တဲ့အခါ အစဉ်လိုက် ရေးစရာ မလိုပါဘူး။

Example: ဈေးလျှော့ တွက်တဲ့ function

ဒီ function က မူလဈေးနဲ့ လျှော့ရာခိုင်နှုန်း ယူပြီး အဆုံးသတ် ဈေးကို return ပြန်ပါတယ်။ ရလဒ်ကို function က မ print ဘဲ ပြန်ပေးတာမို့ caller က echo နဲ့ ပြတာ၊ variable ထဲ သိမ်းတာ၊ နောက်တွက်ချက်မှုထဲ ထည့်တာ ကိုယ်ရွေးလို့ရပါတယ်။ တူညီတဲ့ function ကို argument သုံးမျိုးနဲ့ ခေါ်ပြထားပါတယ်။

discount.php
<?php
function calculateDiscountPrice(float $price, int $discountPercent = 0): float
{
    $discountAmount = $price * ($discountPercent / 100);
    return $price - $discountAmount;
}

echo "Final price: " . calculateDiscountPrice(20000, 15) . " MMK\n";
echo "No discount: " . calculateDiscountPrice(20000) . " MMK\n";
echo "Named: " . calculateDiscountPrice(discountPercent: 50, price: 8000) . " MMK\n";

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Signaturefunction calculateDiscountPrice(float $price, int $discountPercent = 0): float

    Parameter နှစ်ခုကို type ပေးထားပြီး $discountPercent မှာ default 0 ရှိလို့ optional ဖြစ်ပါတယ်။ : float ကတော့ float ပြန်ပေးမယ်ဆိုတဲ့ ကတိပါ။

  2. Local variable$discountAmount = $price * ($discountPercent / 100);

    $discountAmount က function ထဲမှာပဲ ရှိတဲ့ local variable ပါ။ Function ပြီးရင် ပျောက်ပါတယ်၊ အပြင်ကနေ ဒီနာမည်နဲ့ ခေါ်လို့ မရပါဘူး။

  3. returnreturn $price - $discountAmount;

    return က တန်ဖိုးကို caller ဆီ ပို့ပြီး function ကို ပြီးဆုံးစေပါတယ်။ 20000 - 3000 = 17000 ပါ။

  4. Default usedcalculateDiscountPrice(20000)

    Argument တစ်ခုတည်း ပို့ရင် $discountPercent က default 0 ကို သုံးလို့ ဈေးက 20000 အတိုင်း ရှိနေပါတယ်။

  5. Named argumentscalculateDiscountPrice(discountPercent: 50, price: 8000)

    နာမည်နဲ့ ပို့တာမို့ အစဉ်ပြောင်းပြီး ရေးလို့ရပါတယ်။ ဘယ် value က ဘာလဲဆိုတာလည်း ခေါ်တဲ့နေရာမှာတင် ဖတ်လို့ ရပါတယ်။

You should see
Final price: 17000 MMK
No discount: 20000 MMK
Named: 4000 MMK

Type declaration: nullable, union နဲ့ void

Function က တစ်ခါတစ်လေ "ရှာမတွေ့ဘူး" လို့ ပြောရတတ်ပါတယ်။ အဲဒီအခါ ?string လို nullable return type က ကောင်းပါတယ်၊ ခေါ်တဲ့သူက null ဖြစ်နိုင်တယ်ဆိုတာ signature ကိုကြည့်ရုံနဲ့ သိပါတယ်။ int|float လို union type (PHP 8.0 ကစပြီး) က type နှစ်ခုကို လက်ခံပါတယ်။ void function ကတော့ side effect အတွက်ပဲရှိပြီး ရလဒ်ကို variable ထဲ ယူရင် null ရပါတယ်။

types.php
<?php
function findUsername(int $id): ?string
{
    $users = [1 => "Aye", 2 => "Min"];
    return $users[$id] ?? null;
}

function half(int|float $n): float
{
    return $n / 2;
}

function logLine(string $message): void
{
    echo "[log] $message\n";
}

var_dump(findUsername(1));
var_dump(findUsername(9));
var_dump(half(5));
var_dump(half(2.5));
logLine("saved");
$result = logLine("again");
var_dump($result);

ဒီ code က string(3) "Aye"၊ NULL၊ float(2.5)၊ float(1.25) ကို print ထုတ်ပြီး [log] saved၊ [log] again ပြီး နောက်ဆုံး NULL ထွက်ပါတယ်။ ?string ကို ပြန်ပေးတဲ့ function ကို ခေါ်ရင် null ဖြစ်နိုင်တာကို ကိုင်တွယ်ဖို့ ?? ဒါမှမဟုတ် === null စစ်တာ မမေ့ပါနဲ့။

Default value, named argument နဲ့ variadic

Default value ရှိတဲ့ parameter တွေကို required parameter တွေရဲ့ နောက်မှာ ထားပါ။ Optional ကို required ရှေ့မှာ ထားရင် PHP 8 က Deprecated ပြပြီး optional ကို required အဖြစ် ဆက်ဆံပါတယ်။ Named argument ကတော့ default တစ်ခုကို ကျော်ပြီး နောက်ထပ်တစ်ခုကို သတ်မှတ်ချင်တဲ့အခါ အသုံးဝင်ပါတယ်။ Variadic parameter ...$numbers ကို array တစ်ခုအဖြစ် လက်ခံပြီး ခေါ်တဲ့အခါ ...$array နဲ့ array ကို argument တွေအဖြစ် ပြန်ဖြန့်လို့ ရပါတယ်။

arguments.php
<?php
function createUser(string $name, string $role = "member", bool $active = true): string
{
    return $name . " (" . $role . ", " . ($active ? "active" : "inactive") . ")";
}

echo createUser("Aye"), "\n";
echo createUser("Min", "admin"), "\n";
echo createUser("Hla", active: false), "\n";
echo createUser(active: false, name: "Su"), "\n";

function sum(int|float ...$numbers): int|float
{
    return array_sum($numbers);
}

$more = [4, 5, 6];
echo sum(), " ", sum(1, 2, 3), " ", sum(...$more), "\n";

Output က Aye (member, active)၊ Min (admin, active)၊ Hla (member, inactive)၊ Su (member, inactive) ပြီးတော့ 0 6 15 ပါ။ Named argument ရဲ့ နာမည်က function ရဲ့ parameter နာမည်ဖြစ်လို့ နောက်ပိုင်း parameter ကို rename လုပ်ရင် ခေါ်ထားတဲ့ နေရာတွေပါ ပျက်သွားနိုင်တာကို သတိထားပါ။

Closure, arrow function, static နဲ့ reference

Closure ဆိုတာ variable ထဲ သိမ်းလို့ရတဲ့ နာမည်မဲ့ function ပါ။ use ($taxRate) နဲ့ အပြင် variable ရဲ့ တန်ဖိုးကို closure ဖန်တီးတဲ့အချိန်မှာ copy ယူပြီး နောက်ပိုင်း အပြင် variable ပြောင်းလည်း closure ထဲက တန်ဖိုးက မပြောင်းပါဘူး။ use (&$count) လို & ထည့်ရင်တော့ variable အတူတူကို မျှဝေပါတယ်။ Arrow function fn က expression တစ်ခုတည်းအတွက် တိုတိုရေးတာဖြစ်ပြီး use မလိုဘဲ အပြင် variable တွေကို by value နဲ့ ယူပါတယ်။ static variable ကတော့ call တစ်ခုနဲ့တစ်ခု ကြား တန်ဖိုးကို မှတ်ထားပါတယ်။

closures.php
<?php
$taxRate = 0.05;
$withTax = function (float $price) use ($taxRate): float {
    return round($price * (1 + $taxRate), 2);
};

$discount = 10;
$afterDiscount = fn(float $price): float => $price * (1 - $discount / 100);

$taxRate = 0.5;   // changed after the closures were created
$discount = 90;
echo $withTax(100), " ", $afterDiscount(100), "\n";

$count = 0;
$increment = function () use (&$count): void {
    $count++;
};
$increment();
$increment();
echo "count = $count\n";

function nextId(): int
{
    static $id = 0;
    return ++$id;
}
echo nextId(), nextId(), nextId(), "\n";

function addTag(array &$tags, string $tag): void
{
    $tags[] = $tag;
}
$tags = ["php"];
addTag($tags, "forms");
echo implode(",", $tags), "\n";

echo implode(",", array_map(fn(int $n): int => $n * $n, [1, 2, 3])), "\n";

Output က 105 90၊ count = 2၊ 123၊ php,forms၊ 1,4,9 ပါ။ $taxRate နဲ့ $discount ကို နောက်မှ ပြောင်းလည်း 105 နဲ့ 90 က မပြောင်းတာကို သတိထားပါ၊ ဒါက copy ယူထားလို့ပါ။ By reference parameter (array &$tags) ကို literal array နဲ့ ခေါ်ရင် Error: ... could not be passed by reference ထွက်ပါလိမ့်မယ်၊ & ပါတဲ့ parameter ကို variable တစ်ခု ပို့ရပါမယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Real project တွေမှာ function signature က team အတွက် documentation ဖြစ်လာပါတယ်။ formatMoney(int $cents, string $currency = "USD"): string လို့ ဖတ်လိုက်တာနဲ့ implementation မဖွင့်ဘဲ ဘယ်လို ခေါ်ရမလဲ သိပါတယ်။ PHP ရဲ့ built-in function အများစုလည်း callback ယူတဲ့ပုံစံမို့ array_map(), array_filter(), usort() တွေနဲ့ closure ကို တွဲသုံးတာ နေ့စဉ် ရေးရမယ့် code ပါ။ strlen(...) လို့ ရေးရင် built-in function ကိုတောင် callable ပြန်ယူလို့ ရပါတယ်။

helper.php
<?php
function e(string $text): string
{
    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

echo "<p>" . e("Tom & \"Jerry\"") . "</p>\n";

$products = [["name" => "Pen", "price" => 5], ["name" => "Clip", "price" => 2]];
usort($products, fn(array $a, array $b): int => $a["price"] <=> $b["price"]);
echo $products[0]["name"], "\n";

ဒီ helper ကလေးက ကောင်းတဲ့ function ရဲ့ လက္ခဏာကို ပြပါတယ် — အလုပ်တစ်ခုတည်း လုပ်ပြီး ရလဒ်ကို return ပြန်ပေးပါတယ်။ ဒါကြောင့် HTML ထဲ ထည့်မယ့်နေရာမှာပဲ escape လုပ်လို့ ရပါတယ်၊ unit test ရေးရလွယ်ပါတယ်။ Function ထဲမှာ echo လုပ်၊ database ရေး၊ global variable ပြောင်းတာတွေ (side effect) များလေ ပြန်သုံးဖို့နဲ့ test လုပ်ဖို့ ခက်လေပါပဲ၊ ဒါကြောင့် တွက်ချက်တဲ့ အပိုင်းနဲ့ output လုပ်တဲ့ အပိုင်းကို ခွဲထားပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. return အစား echo သုံးတာ

function total() { echo $a * $b; } ကို $x = total(); လို့ ခေါ်ရင် $x က null ပါ။ Screen မှာ ပေါ်နေလို့ အလုပ်လုပ်တယ်လို့ ထင်ရပေမယ့် ရလဒ်ကို နောက်တွက်ချက်မှုမှာ သုံးလို့ မရပါဘူး။ return $a * $b; လို့ ရေးပြီး print ထုတ်တာကို ခေါ်တဲ့နေရာမှာ လုပ်ပါ။

2. အပြင်က variable ကို function ထဲက မြင်မယ်လို့ ထင်တာ

$taxRate ကို function အပြင်မှာ သတ်မှတ်ပြီး function ထဲမှာ သုံးရင် Warning: Undefined variable $taxRate ထွက်ပြီး တန်ဖိုးက null အဖြစ် သုံးခံရပါတယ်။ ပြင်နည်းက parameter အဖြစ် ပို့တာ၊ closure မှာ use ($taxRate) သုံးတာ၊ ဒါမှမဟုတ် arrow function fn ကို သုံးတာပါ။ global $taxRate ကတော့ ဘယ်လို function က ဘာကို မှီခိုနေလဲ ဖုံးကွယ်လို့ ရှောင်ပါ။

3. Argument ကို ပြောင်းရင် caller က မြင်မယ်လို့ ထင်တာ

function bump(array $list) { $list[] = 99; } က caller ရဲ့ array ကို မပြောင်းပါဘူး၊ array ကို by value ပို့လို့ copy တစ်ခုကိုပဲ ပြင်တာပါ။ ပြောင်းထားတဲ့ array ကို return ပြန်ပါ၊ တကယ်ပဲ caller ရဲ့ variable ကို ပြင်ရမယ်ဆိုမှ array &$list နဲ့ reference ပို့ပါ။

4. Optional parameter ကို required ရှေ့မှာ ထားတာ

function f(int $a = 1, int $b) လို့ ရေးရင် PHP 8 က Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter ပြပါတယ်။ Default က အသုံးမဝင်တော့ပါဘူး။ Default ရှိတဲ့ parameter တွေကို အမြဲ နောက်ဆုံးမှာ ထားပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Function တစ်ခုက အလုပ်တစ်ခုပဲ လုပ်စေပြီး နာမည်ကို ကြိယာနဲ့ ပေးပါ (formatPrice, isValidEmail)။
  • Parameter တိုင်းနဲ့ return value မှာ type ရေးပါ၊ ရလဒ်မရှိတတ်ရင် ?type သုံးပြီး မရှိနိုင်တာကို signature မှာ ပြပါ။
  • echo ထက် return ကို ရွေးပြီး HTML ထုတ်တဲ့နေရာမှာပဲ escape လုပ်ပါ။
  • Boolean flag သုံးလေးခု ဆက်တိုက်ပါလာရင် named argument သုံးပါ၊ ဒါမှမဟုတ် array တစ်ခုထဲ စုပါ။

Mini challenge

Mini challenge: ဘီလ် ခွဲတွက်တဲ့ helper များ

သူငယ်ချင်းတွေနဲ့ စားပြီး ဘီလ်ကို ခွဲတွက်ဖို့ function သုံးခု ရေးပြီး ပေါင်းသုံးကြည့်ပါ။ Type ပြည့်စုံပြီး return နဲ့ ရလဒ်ပြန်ပေးရမယ်၊ function ထဲမှာ echo မသုံးရပါ။

  • splitBill(float $total, int $people = 2, float $tipPercent = 10.0): float — tip ပါထည့်ပြီး လူတစ်ယောက်စာကို ဒသမ နှစ်နေရာ round လုပ်ရမယ်
  • maxOrNull(int ...$numbers): ?int — အကြီးဆုံးကို ပြန်ပေးပြီး argument မပါရင် null ပြန်ရမယ်
  • makeMultiplier(int $factor): Closure — use နဲ့ $factor ကို ယူတဲ့ closure ကို ပြန်ပေးရမယ်
  • splitBill ကို named argument နဲ့ တစ်ကြိမ်ခေါ်ပြီး makeMultiplier ကို array_map() နဲ့ သုံးပါ
bill.php
<?php
function splitBill(/* TODO: total, people = 2, tipPercent = 10.0 */): float
{
    // TODO: add the tip, divide by the people, round to 2 decimals
}

function maxOrNull(/* TODO: variadic int parameter */): ?int
{
    // TODO: null when no numbers were passed
}

function makeMultiplier(int $factor): Closure
{
    // TODO: return a function(int $n) that captures $factor with use
}

// TODO: print a default split, a named-argument split, both max cases
// TODO: print array_map(makeMultiplier(3), [1, 2, 3]) joined with ", "

ရလဒ်: ထွက်လာရမယ့် output: Default split: 49.50 Four people, no tip: 30.00 Max: 9, empty: none Tripled: 3, 6, 9

အရိပ်အမြွက် ကြည့်ရန်

Variadic parameter က array တစ်ခုပါ၊ ဒါကြောင့် $numbers === [] နဲ့ အလွတ်ဟုတ်မဟုတ် စစ်ပြီး max($numbers) ကို သုံးပါ။ number_format($value, 2) နဲ့ ဒသမနှစ်နေရာကို တိကျစွာ print ထုတ်နိုင်ပါတယ်။ Named argument နဲ့ splitBill(total: 120, tipPercent: 0, people: 4) လို အစဉ်ပြောင်းရေးပါ။

အဖြေ ကြည့်ရန်
bill.php
<?php
function splitBill(float $total, int $people = 2, float $tipPercent = 10.0): float
{
    return round($total * (1 + $tipPercent / 100) / $people, 2);
}

function maxOrNull(int ...$numbers): ?int
{
    return $numbers === [] ? null : max($numbers);
}

function makeMultiplier(int $factor): Closure
{
    return function (int $n) use ($factor): int {
        return $n * $factor;
    };
}

echo "Default split: " . number_format(splitBill(90.0), 2) . "\n";
echo "Four people, no tip: "
    . number_format(splitBill(total: 120, tipPercent: 0, people: 4), 2) . "\n";
echo "Max: " . (maxOrNull(3, 9, 4) ?? "none")
    . ", empty: " . (maxOrNull() ?? "none") . "\n";
echo "Tripled: " . implode(", ", array_map(makeMultiplier(3), [1, 2, 3])) . "\n";

Quick Quiz 1 / 3

function box(int $w, int $h = 1, int $d = 1): int { return $w * $h * $d; } ရှိရင် echo box(5, d: 3); က ဘာ print ထုတ်မလဲ။

Quick Quiz 2 / 3

$rate = 0.1; $f = fn($p) => $p * $rate; $rate = 0.5; echo $f(100); က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

function counter() { static $n = 0; return ++$n; } ကို counter(); counter(); echo counter(); ဆိုပြီး သုံးခါ ခေါ်ရင် ဘာ print ထုတ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Function က ကိုယ်ပိုင် local scope ရှိပါတယ်၊ အပြင်က တန်ဖိုးကို parameter (ဒါမှမဟုတ် closure ရဲ့ use) နဲ့ ပို့ပြီး ရလဒ်ကို echo မဟုတ်ဘဲ return နဲ့ ပြန်ပေးပါ။
  • Parameter / return type, ?int, int|float, void တွေက function ကို ကတိတစ်ခု ဖြစ်စေပြီး default, named, variadic argument တွေက ခေါ်ရတာ ပြောင်းလွယ်စေပါတယ်။
  • Closure က use နဲ့ တန်ဖိုးကို copy ယူ၊ arrow function fn က အလိုအလျောက် by value ယူ၊ static variable က call တွေကြား တန်ဖိုးကို မှတ်ထားပါတယ်။
  • Argument တွေကို by value ပို့ပြီး caller ရဲ့ variable ကို ပြင်ချင်မှ & (by reference) သုံးပါ။

PHP Language Reference — PHP Documentation Group

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Functions | Thuta Learning