PHP sprintf()Functions
PHP String Reference Manual
Examples
Replace the percent sign (%) with a variable passed as a parameter:
<?php
$number = 9;
$str = "EXAMPLE";
$txt = sprintf("There are %u ten-thousand people visiting %s every day!", $str, $number);
echo $txt;
?>
The output is as follows:
EXAMPLE 每天有 9 万人在访问!
Definition and Usage
The sprintf() function writes a formatted string to a variable.
The arg1, arg2, ++ parameters will be inserted at the percent (%) sign in the main string. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, and so on.
Note:If there are more % signs than arg parameters, you must use placeholders. A placeholder is inserted after the % sign and consists of a number and "\$". See Example 2.
Tip:Related Functions:printf()、
vprintf()、
vsprintf()、
fprintf()andvfprintf()
Syntax
sprintf(format,arg1,arg2,arg++)
| Parameters |
Description |
| format |
Required. Specifies the string and how to format the variables in it. Possible format values:
- %% - Returns a percent sign %
- %b - Binary number
- %c - The character corresponding to the ASCII value
- %d - Signed decimal number (negative, zero, positive)
- %e - Scientific notation using lowercase (e.g. 1.2e+2)
- %E - Scientific notation using uppercase (e.g. 1.2E+2)
- %u - Unsigned decimal number (greater than or equal to 0)
- %f - Floating-point number (local settings)
- %F - Floating-point number (non-local settings)
- %g - Shorter of %e and %f
- %G - Shorter of %E and %f
- %o - Octal number
- %s - String
- %x - Hexadecimal number (lowercase letters)
- %X - Hexadecimal number (uppercase letters)
Additional format values. Must be placed between % and the letter (e.g. %.2f):
- + (Adds + or - before a number to define its sign. By default, only negative numbers are marked, positive numbers are not)
- ' (Specifies what to use as padding, default is space. It must be used together with the width specifier. For example: %'x20s (using "x" as padding))
- - (Left-justifies the variable value)
- [0-9] (Specifies the minimum width of the variable value)
- .[0-9] (Specifies the number of decimal digits or maximum string length)
Note:If multiple format values above are used, they must be used in the order above and cannot be rearranged.
|
| arg1 |
Required. Specifies the parameter to be inserted at the first % sign in the format string. |
| arg2 |
Optional. Specifies the parameter to be inserted at the second % sign in the format string. |
| arg++ |
Optional. Specifies the parameters to be inserted at the third, fourth, etc. % signs in the format string. |
Technical Details
| Return Value: |
Returns the formatted string. |
| PHP Version: |
4+ |
More Examples
Example 1
Using the format value %f:
<?php
$number = 123;
$txt = sprintf("%f",$number);
echo $txt;
?>
Run Example »
Example 2
Using placeholders:
<?php
$number = 123;
$txt = sprintf("With two decimal places: %1\$.2f
<br>Without decimals: %1\$u",$number);
echo $txt;
?>
Run Example »
Example 3
Demonstration of all possible format values:
<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50;
echo sprintf("%%b = %b",$num1)."<br>";
echo sprintf("%%c = %c",$char)."<br>";
echo sprintf("%%d = %d",$num1)."<br>";
echo sprintf("%%d = %d",$num2)."<br>";
echo sprintf("%%e = %e",$num1)."<br>";
echo sprintf("%%E = %E",$num1)."<br>";
echo sprintf("%%u = %u",$num1)."<br>";
echo sprintf("%%u = %u",$num2)."<br>";
echo sprintf("%%f = %f",$num1)."<br>";
echo sprintf("%%F = %F",$num1)."<br>";
echo sprintf("%%g = %g",$num1)."<br>";
echo sprintf("%%G = %G",$num1)."<br>";
echo sprintf("%%o = %o",$num1)."<br>";
echo sprintf("%%s = %s",$num1)."<br>";
echo sprintf("%%x = %x",$num1)."<br>";
echo sprintf("%%X = %X",$num1)."<br>";
echo sprintf("%%+d = %+d",$num1)."<br>";
echo sprintf("%%+d = %+d",$num2)."<br>";
?>
Run Example »
Example 4
Demonstration of string specifiers:
<?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>";
?>
Run Example »
PHP String Reference Manual Other Extensions