printf လုပ်ဆောင်ချက်
printf လုပ်ဆောင်ချက်သည် သတ်မှတ်ထားသော format အတိုင်း format ပြုလုပ်ထားသည့် string တစ်ခုကို ထုတ်ပြသည်။
ပထမဆုံး parameter အဖြစ် format string ကို လက်ခံပြီး၊ နောက်ထပ် parameter များသည် အစားထိုးရန် တန်ဖိုးများဖြစ်သည်။ Format string အတွင်း၌ အထူးသင်္ကေတများ (format specifiers) ကို အသုံးပြုထားပြီး ၎င်းတို့သည် % သင်္ကေတဖြင့် စတင်ကာ ရလဒ်ထုတ်ပေးမှု၏ format ကို ထိန်းချုပ်ပေးသည်။
Syntax
printf(string $format, mixed ...$values): int
Format Specifiers
| Specifier | ဖော်ပြချက် |
|---|---|
%s |
String |
%d |
Signed integer (decimal) |
%u |
Unsigned integer (decimal) |
%f |
Floating point number (locale-aware) |
%F |
Floating point number (non-locale-aware) |
%c |
ASCII code ဖြင့် character |
%x |
Integer (hexadecimal - lowercase) |
%X |
Integer (hexadecimal - uppercase) |
%o |
Integer (octal) |
%b |
Integer (binary) |
%e |
Scientific notation (lowercase) |
%E |
Scientific notation (uppercase) |
%g |
Short %e or %f |
%G |
Short %E or %F |
%% |
Percent sign |
ဥပမာ
ဤဥပမာတွင် %s အစား
စကားလုံး 'apples' ကို ထည့်ပေးမည်ဖြစ်ပြီး
%d အစား ဂဏန်း
3 ကို ထည့်ပေးမည်ဖြစ်သည်:
<?php
$product = 'apples';
$num = 3;
printf('product - %s, amount - %d', $product, $num);
?>
ကုဒ်ကို run ပြီးရလဒ်:
'product - apples, amount - 3'
ဥပမာ
ဤဥပမာတွင် parameter များကို နေရာလဲထားပြီး numerical order ကို ထည့်သွင်းထားသည်:
<?php
$num = 3;
$product = 'apples';
printf('product - %2$s, amount - %1$d', $num, $product);
?>
ကုဒ်ကို run ပြီးရလဒ်:
'product - apples, amount - 3'
ဥပမာ
ဤဥပမာတွင် တူညီသော ပမာဏကို အကြိမ်ကြိမ်အခါခါ လိုအပ်သောကြောင့် numerical order ကို ထည့်သွင်းထားသည်:
<?php
$num = 3;
$product = 'apples';
printf('product - %1$s, amount - %2$d, count - %2$d', $product, $num);
?>
ကုဒ်ကို run ပြီးရလဒ်:
'product - apples, amount - 3, count - 3'
ဆက်လက်လေ့လာရန်
-
sprintfလုပ်ဆောင်ချက်,
အလားတူလုပ်ဆောင်ချက်ကို ရလဒ်ပြန်ပေးခြင်းဖြင့် လုပ်ဆောင်ပေးသည် -
number_formatလုပ်ဆောင်ချက်,
ဂဏန်းတစ်ခုကို format ပြုလုပ်ပေးသည်