Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท String Functions

PHP sprintf() function

Reference for PHP sprintf(): format strings with placeholders, padding, precision, argument numbering, the type specifiers and the errors it throws in PHP 8.

sprintf() builds a string from a format template and a list of values. Each placeholder in the template starts with % and describes how the next value should be converted: as a decimal integer, a float with a fixed number of decimals, a padded string, a hexadecimal number and so on. The function returns the result and prints nothing, which makes it the right tool whenever you want to store, return or escape text before it is shown.

A placeholder has the shape %[argnum$][flags][width][.precision]specifier. Only the percent sign and the specifier are required. To print a literal percent sign, write %%.

PHP
<?php
echo sprintf("%s has %d items (%.1f%%)", "Cart", 3, 42.456);
// Cart has 3 items (42.5%)

Try it

PHP Common specifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Numbered arguments and tables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
sprintf(string $format, mixed ...$values): string
Name Description
$format The template. Ordinary characters are copied; % sequences are replaced by the values.
...$values The values to insert, in order, unless a placeholder uses n$ to pick a specific one.

Return value: the formatted string.

Errors: an ArgumentCountError if there are fewer values than placeholders, and a ValueError for an unknown format specifier or an invalid argument number such as %0$s.

Specifiers

Specifier Meaning
%s String. The value is converted to a string first.
%d Signed decimal integer.
%u Unsigned decimal integer (negative numbers wrap around).
%f, %F Float; %f respects the locale decimal point, %F does not. Six decimals by default.
%e, %E Scientific notation.
%g, %G Shorter of %e and %f.
%x, %X Hexadecimal, lower or upper case.
%o Octal.
%b Binary.
%c The character with that byte value.
%% A literal percent sign.

Flags, width and precision

Part Meaning
- Left-justify inside the width.
+ Always show a sign on numbers.
0 Pad numbers with zeros. Space is the default padding.
'c Use the character c as padding.
width Minimum number of characters.
.precision Decimals for floats; maximum characters for %s.

Usage notes

  • Use sprintf() to keep text and layout apart. One readable template beats long chains of concatenation, and the same template can be reused.
  • Argument numbers with n$. In double-quoted PHP strings the dollar sign starts a variable, so write \$ as in "%2\$s", or use single quotes: '%2$s'. Once you use numbered placeholders, use them everywhere in that template.
  • %d truncates, it does not round. sprintf("%d", 3.99) gives 3. Use %.0f or round() if you want rounding. Non-numeric strings become 0.
  • %f rounds to the requested precision. Floats are stored in binary, so a value that looks like a tie in decimal (such as 1.005) may round in a way you did not expect. Test edge cases, and keep money in integer cents when exactness matters.
  • %s with floats uses the normal string conversion, so use %.2f for money, never %s.
  • Locale. %f depends on the current locale, so a German locale could print a comma. %F always uses a dot; prefer it when you build machine-readable text.
  • Multibyte text. Width and precision count bytes, not characters. Padding "รฉ" to width 5 yields only 3 spaces. Use mb_str_pad() (PHP 8.3+) or compute the padding yourself for UTF-8 tables.
  • Never put user input in the format string. A string like "%s%s%s" from a user would raise an error at best. Pass user data as values.
  • Arrays of values: use vsprintf() when the values are already in an array.
  • Number formatting for people: number_format() adds thousands separators, which sprintf() cannot do.