PHP တွင် Sprintf နှင့် Printf လုပ်ဆောင်ချက်များသည်များသောအားဖြင့် string formating tools များကိုအသုံးပြုကြသည်။ သူတို့က output ကိုပိုပြီးပြောင်းလွယ်ပြင်လွယ်နှင့်ထိန်းချုပ်ရန်လွယ်ကူစေသည်။ နှစ်ခုသည်အချို့သောနည်းလမ်းများနှင့်ဆင်တူသော်လည်းသူတို့၏အသုံးပြုမှုအခြေအနေများကွဲပြားခြားနားသည်။ ဒီဆောင်းပါးကဒီလုပ်ငန်းစဉ်ကို terminal ထဲကလုပ်ဆောင်ချက်နှစ်ခုကိုဘယ်လိုသုံးရမလဲဆိုတာရှင်းပြပါလိမ့်မယ်။
Sprintf function ကိုတိုက်ရိုက်ထုတ်ပယ်ခြင်းမရှိဘဲ format ချထားသော string ကိုပြန်ပို့ရန်အသုံးပြုသည်။ ရလဒ်ကိုမျက်နှာပြင်သို့ပုံနှိပ်ခြင်းမပြုပါ။
သဒ္ဒါ:
sprintf(string $format, mixed ...$values): string
$ format - format control control control ပါ 0 င်သည့် string တစ်ခု။
$ တန်ဖိုးများ - format character ကိုအစားထိုးရန်တန်ဖိုး။
ဥပမာ -
$name = "John";
$age = 25;
$formattedString = sprintf("My name is %s and I am %d years old.", $name, $age);
echo $formattedString;
ထုတ်လုပ်မှု -
My name is John and I am 25 years old.
Sprintf နှင့်ဆင်တူသည်မှာ printf function ကိုလည်း output ကို format လုပ်ရန်လည်းအသုံးပြုသည်။ ခြားနားချက်မှာ Printf သည် format ချထားသော string ကို terminal သို့တိုက်ရိုက်ပြန်ပို့မည့်အစား terminal သို့တိုက်ရိုက်ထုတ်ဝေသည်။
သဒ္ဒါ:
printf(string $format, mixed ...$values): int
$ format - format control control control ပါ 0 င်သည့် string တစ်ခု။
$ တန်ဖိုးများ - format character ကိုအစားထိုးရန်တန်ဖိုး။
ဥပမာ -
$name = "John";
$age = 25;
printf("My name is %s and I am %d years old.", $name, $age);
ထုတ်လုပ်မှု -
My name is John and I am 25 years old.
တစ်ခါတစ်ရံတွင်ကျွန်ုပ်တို့သည် format ချထားသည့် string ကို variable တစ်ခုထဲသို့သိမ်းဆည်းရန်လိုအပ်ပြီး၎င်းကို Printf မှတစ်ဆင့်ထုတ်ယူပါသို့မဟုတ် Printf မှတစ်ဆင့်တစ်စိတ်တစ်ပိုင်း format လုပ်ထားသောအကြောင်းအရာများကိုထုတ်လုပ်ရန်လိုအပ်သည် ။
ဥပမာ -
$name = "John";
$age = 25;
// အသုံးပြု sprintf format string ကို
$formattedString = sprintf("My name is %s and I am %d years old.", $name, $age);
// formatted string ကိုသုံးပါ printf ထုတ်လုပ်ခြင်း
printf("Formatted String: %s\n", $formattedString);
ထုတ်လုပ်မှု -
Formatted String: My name is John and I am 25 years old.
အချို့ဖြစ်ရပ်များတွင် URL ပါ 0 င်သည့် output ကို format လုပ်ရန်လိုအပ်ပြီး URL တွင် domain name ကိုပြုပြင်ရန်လိုအပ်သည်။ URLs များဖြင့် Dynamically string များကို dynamically string များကိုထုတ်လွှင့်ရန် sprintf ကို သုံးနိုင်သည်။ ထို့နောက် output ကို output ကိုသုံးပါ။
URL ပါ 0 င်သည့် link တစ်ခုနှင့် gitbox.net နှင့်အတူ domain name ကိုအစားထိုးလိုသည်ဆိုပါစို့။
ဥပမာ -
$originalUrl = "https://example.com/path/to/resource";
$formattedUrl = sprintf("https://%s%s", "gitbox.net", substr($originalUrl, strpos($originalUrl, "/")));
printf("The formatted URL is: %s\n", $formattedUrl);
ထုတ်လုပ်မှု -
The formatted URL is: https://gitbox.net/path/to/resource
ဤဥပမာတွင် Sprintf function သည် Gitintf function ကို URL သို့ထည့်သွင်းထားသည်။
Sprintf နှင့် Printf များကို ပေါင်းစပ်ခြင်းအားဖြင့်သင်သည်ကြိုးများပိုမိုပြောင်းလွယ်ပြင်လွယ်ရှိသောကြိုး၏ပုံစံနှင့်ထုတ်လုပ်မှုကိုသင်ထိန်းချုပ်နိုင်သည်။ Sprintf သည် format ချထားသောကြိုးများကိုထုတ်လုပ်ရန်နှင့်နောက်ပိုင်းတွင်အသုံးပြုရန်အတွက်သင့်တော်သည်။ Printf သည် format လုပ်ထားသောအကြောင်းအရာများကိုတိုက်ရိုက်ထုတ်လွှင့်ရန်အတွက်သင့်လျော်သည်။ ပေါင်းစပ်အသုံးပြုသောအခါသင်သည် URLs ကို format လုပ်ရန်သို့မဟုတ် dynamic content နှင့်အတူမက်ဆေ့ခ်ျများတည်ဆောက်ခြင်းကဲ့သို့သောရှုပ်ထွေးသောအလုပ်များကိုပြီးမြောက်စေနိုင်သည်။