sprintf() builds a string from a format template and returns it, while printf() does the same but prints it straight to the output. A format template contains placeholders that start with %, such as %s for a string, %d for an integer and %.2f for a float with two decimals. Learning php sprintf printf formats lets you pad numbers, align columns and control decimals without manual string joining.
This guide explains the placeholder syntax, padding and alignment flags, argument swapping, the related number_format(), and the safety pitfalls to avoid. Examples target PHP 8.3 or newer.
Each placeholder consumes one argument, in order. The most common conversion specifiers are listed below.
| Specifier | Meaning | Example input | Output |
|---|---|---|---|
%s | string | 'Asha' | Asha |
%d | signed integer | 42 | 42 |
%u | unsigned integer | 7 | 7 |
%f | float (six decimals by default) | 3.14159 | 3.141590 |
%.2f | float, two decimals | 3.14159 | 3.14 |
%x / %X | hexadecimal | 255 | ff / FF |
%b | binary | 5 | 101 |
%o | octal | 8 | 10 |
%e | scientific notation | 12345.678 | 1.234568e+4 |
%% | a literal percent sign | none | % |
printf() returns the length of the printed string, and sprintf() returns the string itself. Use sprintf() whenever you need the result in a variable, for example to store, log or return it.
Between the % and the specifier you can place flags, a width and a precision, in this order: flag, width, .precision.
A zero flag gives zero padding; a single quote followed by any character sets a custom pad character. For a string, precision is the maximum number of characters, which is a quick way to truncate. Width counts bytes, so padding with multibyte text may look uneven.
Because width flags pad the output, they are ideal for plain-text reports in the terminal.
The destructuring [$item, $qty, $price] in the foreach keeps the loop readable.
You can refer to an argument by position using %1$s. This lets you reuse a value or reorder placeholders, which is useful for translated text where word order changes.
Do not mix numbered and plain placeholders in one template. Also be aware that the number of arguments must be at least the number the template needs; since PHP 8.0 a missing argument throws an ArgumentCountError.
%.2f is correct for decimals, but it does not add thousands separators. For that, use number_format(). For locale-aware currency, use the intl extension.
The NumberFormatter class requires the intl extension to be installed. Floats are binary approximations, so for real financial calculations store amounts as integer minor units such as paise or cents and format them only for display.
printf($userInput) can throw errors or print unexpected data when the input contains %. Use printf('%s', $userInput) instead.sprintf() does not escape anything for HTML or SQL. Use htmlspecialchars() for output and prepared statements for queries.vsprintf($format, $array) or vprintf() when the arguments are already in an array.%d truncates a float toward zero, so sprintf('%d', 3.99) gives 3. Use round() first if that is not what you want.printf() prints the formatted text and returns its length. sprintf() returns the formatted text as a string and prints nothing.
Use sprintf('%.2f', $value) or number_format($value, 2). The second also adds thousands separators.
Use sprintf('%05d', $n) for a width of five digits, for example an invoice number such as 00042.
Write %% in the template, because a single % starts a placeholder.
sprintf() returns a formatted string, printf() prints it, and vsprintf() takes an array of values. Learn the common specifiers %s, %d, %f, %x and %b, add flags for padding, alignment and precision, and use numbered arguments when order may change. Use number_format() for thousands separators, and never let user input become the format string.