PHP sprintf() Function
The PHP sprintf() function returns a string produced according to the specified format. If format includes format specifiers (sub-sequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers.
After the format parameter, the function expects additional arguments at least as many as the number of format specifiers in the format string.
Syntax
sprintf(format, arguments)
Parameters
format |
Additional format values can be placed between the % and the specifier (e.g. %.3f). The possible values which can be placed in between are:
Note: If multiple additional format values are provided, they must be in %[flags][width][.precision]specifier order. | ||||||||||||||||||||||||||||||||||||
arguments |
Optional. Depending on the format string, a sequence of additional arguments should be passed in the function, each containing a value to replace a format specifiers in the format string. Number of arguments should be at least equal to the number of format specifiers in the format string. Additional arguments will be ignored by this function. |
Return Value
Returns a string produced according to the specified format string.
Example: different specifiers
In the example below, formatted strings are produced using this function and displayed using echo function.
<?php $n = 5000; $u = -5000; $c = 66; // ASCII 66 is 'B' //%% is used to print '%' character echo sprintf("%%b = %b\n", $n); //binary echo sprintf("%%c = %c\n", $c); //ASCII character echo sprintf("%%d = %d\n", $n); //integer echo sprintf("%%e = %e\n", $n); //scientific notation echo sprintf("%%u = %u\n", $n); //unsigned integer (+ve number) echo sprintf("%%u = %u\n", $u); //unsigned integer (-ve number) echo sprintf("%%f = %f\n", $n); //floating point echo sprintf("%%o = %o\n", $n); //octal echo sprintf("%%s = %s\n", $n); //string echo sprintf("%%x = %x\n", $n); //hexadecimal (lowercase) echo sprintf("%%X = %X\n", $n); //hexadecimal (uppercase) echo sprintf("%%+d = %+d\n", $n); //sign specifier (+ve number) echo sprintf("%%+d = %+d\n", $u); //sign specifier (-ve number) ?>
The output of the above code will be:
%b = 1001110001000 %c = B %d = 5000 %e = 5.000000e+3 %u = 5000 %u = 18446744073709546616 %f = 5000.000000 %o = 11610 %s = 5000 %x = 1388 %X = 1388 %+d = +5000 %+d = -5000
Example: using multiple % signs
In the example below, multiple % signs are used to place multiple additional arguments in the returned string.
<?php $name = 'John'; $age = 25; $result1 = sprintf("%s is %d years old.\n", $name, $age); echo $result1; //using precision format $result2 = sprintf("%s is %.2f years old.\n", $name, $age); echo $result2; ?>
The output of the above code will be:
John is 25 years old. John is 25.00 years old.
Example: format date string
In the example below, this function is used to return formatted date string.
<?php $year = 2015; $month = 5; $day = 1; //each format specifier specifies padding //with 0 and width of the field $result = sprintf("%04d-%02d-%02d", $year, $month, $day); echo $result; ?>
The output of the above code will be:
2015-05-01
Example: string specifiers
Consider one more example to see how to use string specifiers with a given string.
<?php $x = 'catfish'; $y = 'many catfishes'; echo sprintf("[%s]\n", $x); //standard string echo sprintf("[%10s]\n", $x); //right-justification with spaces echo sprintf("[%-10s]\n", $x); //left-justification with spaces echo sprintf("[%010s]\n", $x); //zero-padding works on strings too echo sprintf("[%'#10s]\n", $x); //using custom padding character '#' echo sprintf("[%10.8s]\n", $y); //right-justification (8 characters cutoff) echo sprintf("[%-10.8s]\n", $y); //left-justification (8 characters cutoff) ?>
The output of the above code will be:
[catfish] [ catfish] [catfish ] [000catfish] [###catfish] [ many cat] [many cat ]
❮ PHP String Reference