English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The sprintf() function is used to return a formatted string.
string sprintf ( string $format [, mixed $args [, mixed $... ]])
It is used to format strings, and write the formatted string into a variable.
arg1, arg2,++ The parameter will be inserted at the percentage (%) symbol in the main string. The function is executed step by step. Insert arg at the first % symbol.1Insert arg at the second % symbol.2and so on.
Note:If the % symbol is more than the arg parameter, you must use placeholders. Placeholders are inserted after the % symbol, consisting of numbers and "\$". See examples. 2.
Tip:Related functions:printf(),vprintf(),vsprintf(),fprintf() andvfprintf()
It returns the formatted string
Number | Parameter and description |
---|---|
1 | format Required. It specifies the string and how to format the variable within it Possible format values include:
Additional format values. Must be placed between % and the letter (for example %.2f):
Note:If multiple of the above format values are used, they must be used in the order specified above and cannot be mixed up. |
2 | arg1 Required. It specifies the first parameter to be inserted into the formatted string |
3 | arg2 Optional. It specifies the second parameter to be inserted into the formatted string |
... | arg++ Optional. Specifies the parameter to be inserted at the third, fourth, fifth, and so on, %% symbols in the format string. |
Try the following example, replacing the symbol %f with a variable passed as a parameter:
<?php //Replace the symbol %f with a variable passed as a parameter $number = 123; $txt = sprintf("%f", $number); echo $txt; ?>Test and see‹/›
The result should be as follows
123.000000
Demonstration examples of all possible format values of the parameter format:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII character 50 is 2 // Note: The format value "%%" returns a percentage sign echo sprintf("%%b = %%b", $num1)."<br>"; // Binary number echo sprintf("%%c = %%c", $char)."<br>"; // ASCII character echo sprintf("%%d = %d",$num1)."<br>"; // echo sprintf("%%d = %d",$num echo sprintf("%%d = %d",$num2)."<br>"; // echo sprintf("%%d = %d",$num Signed decimal number1)."<br>"; // Scientific notation (lowercase) echo sprintf("%%E = %E",$num1)."<br>"; // Scientific notation (uppercase) echo sprintf("%鉾u",$num1)."<br>"; // Unsigned decimal number (positive) echo sprintf("%鉾u",$num2)."<br>"; // Unsigned decimal number (negative) echo sprintf("%%f = %f",$num1)."<br>"; // Floating-point number (considering local settings) echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not considering local settings) echo sprintf("%%g = %g",$num1)."<br>"; // Shorter than %e and %f echo sprintf("%%G = %G",$num1)."<br>"; // Shorter than %E and %f echo sprintf("%%o = %o",$num1)."<br>"; // Octal number echo sprintf("%%s = %s",$num1)."<br>"; // String echo sprintf("%%x = %x",$num1)."<br>"; // Hexadecimal number (lowercase) echo sprintf("%%X = %X",$num1)."<br>"; // Hexadecimal number (uppercase) echo sprintf("%%+d = %+d",$num1)."<br>"; // Sign specifier (positive) echo sprintf("%%+d = %+d",$num2)."<br>"; // Sign specifier (negative) ?>Test and see‹/›
The result should be as follows
%b = 111010110111100110100010101 %c = 2 %d = 123456789 %d = -123456789 %e = 1.234568e+8 1.234568E+8 頻, 123456789 頻, 18446744073586094827 %f = 123456789.000000 %F = 123456789.000000 %g = 1.23457e+8 %G = 1.23457E+8 %o = 726746425 %s = 123456789 %x = 75bcd15 %X = 75BCD15 %+d = +123456789 %+d = -123456789
Demonstration examples of string specifiers:
<?php $str1 = "Hello"; $str2 = "Hello PHP!"; echo sprintf("[%s]",$str1)."<br>"; echo sprintf("[%8s]",$str1)."<br>"; echo sprintf("[%-8s]",$str1)."<br>"; echo sprintf("[%08s]",$str1)."<br>"; echo sprintf("[%*8s]",$str1)."<br>"; echo sprintf("[%8.8s]",$str2)."<br>"; ?>Test and see ‹/›
Output Result:
[Hello] [ Hello] [Hello ] [000Hello] [***Hello] [Hello PH]