(PHP 4, PHP 5, PHP 7, PHP 8) sprintf — Return a formatted string Descriptionsprintf(string Parametersformat The format string is composed of zero or more directives: ordinary characters (excluding A conversion specification follows this prototype: Argnum An integer followed by a dollar sign
WidthAn integer that says how many characters (minimum) this conversion should result in. Precision A period
Specifiers
Warning The Warning Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results Variables will be co-erced to a suitable type for the specifier: Type Handling
values Return Values Returns a string produced according to the formatting string Changelog
ExamplesExample #1 Argument swapping The format string supports argument numbering/swapping.
The above example will output: There are 5 monkeys in the tree However imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as:
We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead:
An added benefit is that placeholders can be repeated without adding more arguments in the code.
When using argument swapping, the
Example #2 Specifying padding character
The above example will output: Example #3 Position specifier with other specifiers
The above example will output: The tree contains 0005 monkeys Example #4 sprintf(): zero-padded integers
Example #5 sprintf(): formatting currency
The above example will output: Example #6 sprintf(): scientific notation
sprintf("%.3e", $number); The above example will output: See Also
remy dot damour at -please-no-spam-laposte dot net ¶ 13 years ago
Alex R. Gibbs ¶ 9 years ago
timo dot frenay at gmail dot com ¶ 11 years ago
kontakt at myseosolution dot de ¶ 7 years ago
Anderson ¶ 2 years ago
printf("%%b = '%b'\n", $n); // binary representation dwieeb at gmail dot com ¶ 12 years ago
Anonymous ¶ 5 years ago
viktor at textalk dot com ¶ 13 years ago
$type == '') { no dot email dot address at example dot com ¶ 20 years ago
splogamurugan at gmail dot com ¶ 13 years ago
php at sharpdreams dot com ¶ 18 years ago
jfgrissom at gmail dot com ¶ 13 years ago
abiltcliffe at bigfoot.com ¶ 20 years ago
Pacogliss ¶ 17 years ago
nate at frickenate dot com ¶ 12 years ago
vsprintf($format, array_values($args)); Hayley Watson ¶ 10 years ago
= '%1$s %1$\'#10s %1$s!';printf($pattern, "badgers"); john at jbwalker dot com ¶ 8 years ago
ian dot w dot davis at gmail dot com ¶ 17 years ago
php at mikeboers dot com ¶ 14 years ago
preg_match('/%\(([a-zA-Z0-9_ -]+)\)/', $string, $m)) carmageddon at gmail dot com ¶ 11 years ago
nmmm at nmmm dot nu ¶ 7 years ago
hdimac at gmail dot com ¶ 8 years ago
Nathan Alan ¶ 5 years ago
krzysiek dot 333 at gmail dot com - zryty dot hekko dot pl ¶ 11 years ago
decode_ip($int_ip) jrpozo at conclase dot net ¶ 17 years ago
geertdd at gmail dot com ¶ 12 years ago
Astone ¶ 13 years ago
ignat dot scheglovskiy at gmail dot com ¶ 10 years ago
= "The Books\n"; scott dot gardner at mac dot com ¶ 14 years ago
John Walker ¶ 13 years ago
2838132019 at qq dot com ¶ 1 year ago
Andrew dot Wright at spamsux dot atnf dot csiro dot au ¶ 20 years ago
Anonymous ¶ 7 months ago
Mirek Z... ¶ 2 years ago
ivan at php dot net ¶ 8 years ago
Sam Bull ¶ 6 years ago
|