လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> အရွယ်အစား function ကိုနှင့် Printf အကြားခြားနားချက်အပေါ်ခွဲခြမ်းစိတ်ဖြာခြင်း

အရွယ်အစား function ကိုနှင့် Printf အကြားခြားနားချက်အပေါ်ခွဲခြမ်းစိတ်ဖြာခြင်း

gitbox 2025-06-04

PHP တွင် output ကို format လုပ်ခြင်းသည်အထူးသဖြင့် variable များကိုအထူးသဖြင့် variable များကိုတိကျသောကြိုးနှင့် output များသို့ format ချရန်လိုအပ်သည်။ PHP သည် format လုပ်ထားသော output function များကိုအမျိုးမျိုးသော format formation များကိုထောက်ပံ့ပေးသည် သူတို့အားလုံး output ကို format လုပ်ဖို့အသုံးပြုပေမယ့်သိမ်မွေ့ကွဲပြားခြားနားမှုရှိပါတယ်။ သူတို့၏ကွဲပြားခြားနားမှုများကိုနားလည်ခြင်းသည်အမှန်တကယ်ဖွံ့ဖြိုးတိုးတက်မှုအတွက်မှန်ကန်သောလုပ်ဆောင်မှုကိုရွေးချယ်ပြီးကုဒ်ပြောင်းလွယ်ပြင်လွယ်မှုနှင့်ထိန်းသိမ်းမှုကိုတိုးတက်စေသည်။


Printf () function ကိုမိတ်ဆက်

Printf () PHP တွင်အသုံးပြုသော format လုပ်ထားသည့် output function များထဲမှတစ်ခုဖြစ်သည်။

 <?php
printf("Hello, %s! You have %d new messages.", "Alice", 5);
?>

output ရလဒ် -

 Hello, Alice! You have 5 new messages.

အဓိကအင်္ဂါရပ်များ

  • Printf ၏ပထမ ဦး ဆုံး parameter သည် () ၏ပထမ ဦး ဆုံး parameter သည် format string ကို tothing string ကိုနောက်တော်သို့လိုက်သော pariable parametersters ။

  • placeholders ( % s , % d,% d , စသည်တို့) အဖြစ်) output variable ၏အနေအထားနှင့်အမျိုးအစားကိုကိုယ်စားပြုရန် format string ကို format string ကိုအသုံးပြုကြသည်။

  • Printf () format လုပ်ထားသော string ကိုတိုက်ရိုက်ထုတ်လွှင့်ပြီး output character ၏အရှည်ကိုပြန်ပို့သည်။


VintermfF () function ကိုမိတ်ဆက်

Vintintf () သည် Printf () နှင့်ဆင်တူသော်လည်းကွဲပြားခြားနားသော parameterscers ပုံစံများကိုလက်ခံသည်။ ၎င်း၏ parameter သည်ဖွဲ့စည်းပုံမှာ:

 <?php
$args = ["Alice", 5];
vprintf("Hello, %s! You have %d new messages.", $args);
?>

output ရလဒ်သည်အတူတူပင်ဖြစ်သည်။

 Hello, Alice! You have 5 new messages.

အဓိကအင်္ဂါရပ်များ

  • ပထမ phinmintf ၏ပထမ ဦး ဆုံး parameter သည် format string ကို format string ဖြစ်ပြီးဒုတိယ parameter သည် format string ကိုထည့်သွင်းရန်တန်ဖိုးများအားလုံးပါ 0 င်သည်။

  • ဤဒီဇိုင်းသည် parameters အရေအတွက်ပြောင်းလဲခြင်းသို့မဟုတ်သတ်မှတ်ချက်များကိုခင်းကျင်းထားသည့်အခါခေါ်ဆိုခြင်းအတွက်ခေါ်ဆိုခြင်းအတွက်အကောင်းဆုံးဖြစ်သည်။

  • ထို့အပြင် output ဇာတ်ကောင်အရှည်ကိုပြန်ပို့သည်။


နှစ်ခုအကြားခြားနားချက်၏အကျဉ်းချုပ်

သီးခြားထင်ရှားသော Printf () Vintrintf ()
parameter သည်ပုံစံ parameters တွေကို format + variable ကိုအရေအတွက် format string + parameter သည်ခင်းကျင်း
ကြည့်ရှုပါ fixed parameters တွေကိုတိုက်ရိုက် parameters တွေကိုတိုက်ရိုက် parameters တွေကို array မှာသိမ်းထားတဲ့နှင့် parameters တွေကိုအရေအတွက်ပြောင်းလဲနေတဲ့ဖြစ်ပါတယ်
Value ကိုပြန်သွားပါ output string ကို၏အရှည် output string ကို၏အရှည်
Code Readability ၎င်းသည်တိုက်ရိုက်ရှင်းရှင်းလင်းလင်းရှင်းရှင်းလင်းလင်းရှိပြီး parameters တွေကိုတစ် ဦး အားတ ဦး တည်းစာရင်းဝင်နေကြသည် dynamic parameter သည်သင့်လျော်သော parameter အစု,

Code Support နှိုင်းယှဉ်ခြင်း

 <?php
// အသုံးပြု printf
$name = "Bob";
$count = 3;
printf("User %s has %d notifications.", $name, $count);

// အသုံးပြု vprintf
$params = ["Bob", 3];
vprintf("User %s has %d notifications.", $params);
?>

နှစ်ခုလုံး၏ရလဒ်များအတူတူပင်ဖြစ်ကြသည်:

 User Bob has 3 notifications.

ဘယ်အချိန်မှာ vinrintf ကိုအသုံးပြုမလဲ။

သင့်တွင် parrays array format နဲ့လုပ်ပြီး output ကို format လုပ်ဖို့လိုတယ်ဆိုပါစို့။

 <?php
function notifyUser($format, $args) {
    vprintf($format, $args);
}

notifyUser("Dear %s, your balance is %.2f.", ["Alice", 123.45]);
?>

ဤနေရာတွင် Vintermrf () ကို အသုံးပြုခြင်းသည်လုပ်ဆောင်မှုကိုပြောင်းလဲစေနိုင်သည်။


ကောက်ချက်

Printf () နှင့် အရွယ်အစားပါ 0 င်ခြင်း () PHP ရှိ output output tools နှစ်ခုလုံးသည်အင်အားကြီးမားသော format လုပ်သည့်ကိရိယာများဖြစ်သည်။ မည်သည့်လုပ်ဆောင်ချက်သည်သင်၌ရှိသော parameter form လုပ်ခြင်းသည်မည်သည့်လုပ်ဆောင်ချက်ဖြစ်သည်။

  • အကယ်. parameters တွေကို fixed နှင့်လွတ်လပ်စွာကူးပြောင်းလျှင်, အတိုချုပ်နှင့်အလိုလိုသိသော printf () ကို အသုံးပြုရန်အကြံပြုသည်။

  • အကယ်. parameters တွေကို array ပုံစံသို့မဟုတ် parameters အရေအတွက်ပြောင်းလဲသွားလျှင် vinrincury, vin ရာကွက် ပြောင်းလဲသွားတယ်။

၎င်းတို့ကိုအသုံးပြုခြင်းနှင့်ပြောင်းလွယ်ပြင်လွယ်များကိုအသုံးပြုခြင်းသည် PHP format လုပ်ထားသည့် output ကိုပိုမိုထိရောက်စွာနှင့်ကြော့ရှင်းစေနိုင်သည်။