PHP sprintf() Function
Example
Replace the percentage sign (%) symbol with a variable passed as a parameter:
<?php $number = 2; $str = "Shanghai"; $txt = sprintf("There are 쥕lion cars in %s.", $number, $str); echo $txt; ?>
Definition and usage
The sprintf() function writes the formatted string into a variable.
arg1,arg2,++ The parameters will be inserted into the main string at the percentage (%) symbols. This function is executed step by step. At the first % symbol, insert arg1, insert arg2, and so on.
Note:If there are more % symbols than arg If there are more parameters, you must use placeholders. Placeholders are located after the % symbol and consist of numbers and '\$'. See example 2.
Tip:Related functions: printf(), vprintf(), vsprintf(), fprintf(), and vfprintf()
Syntax
sprintf(format,arg1,arg2,arg++)
Parameter | Description |
---|---|
format |
Required. Specifies the string and how to format the variables within it. Possible format values:
Additional format values. Must be placed between % and the letter (e.g., %.2f):
Note:If multiple of the above format values are used, they must be used in the order listed above. |
arg1 | Required. Specifies what to insert into format The parameter at the first % symbol in the string. |
arg2 | Optional. Specifies what to insert into format The parameter at the second % symbol in the string. |
arg++ | Optional. Specifies what to insert into format The parameters at the third and fourth % symbols in the string. |
Technical details
Return value: | Returns a formatted string. |
PHP Version: | 4+ |
More examples
Example 1
Using format specifier %f:
<?php $number = 123; $txt = sprintf("%f",$number); echo $txt; ?>
Example 2
Using placeholders:
<?php $number = 123; $txt = sprintf("With two decimal places:%1$.2f <br>No decimal: %1$u,$number); echo $txt; ?>
Example 3
Demonstration of all possible format specifiers:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII character 50 is 2 // Comment: The format specifier "%%" returns a percent sign echo sprintf("%%b = %b",$num1)."<br>"; // Binary number echo sprintf("%%c = %c",$char)."<br>"; // ASCII character echo sprintf("%%d = %d",$num1)."<br>"; // Signed decimal number echo sprintf("%%d = %d",$num2)."<br>"; // Signed decimal number echo sprintf("%%e = %e",$num1)."<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 (affected by locale settings) echo sprintf("%%F = %F",$num1)."<br>"; // Floating point number (not affected by locale 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>"; // Positive sign specifier echo sprintf("%%+d = %+d", $num2) . "<br>"; // Negative sign specifier ?>
Example 4
Demonstration of string literals:
<?php $str1 = "Hello"; $str2 = "Hello world!"; 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>"; ?>