printf()
Our article is about the PHP function printf(), which is used to output a formatted string. This function is similar to the echo() function in PHP, but allows
The PHP printf() function outputs a string formatted according to a template. Unlike echo, which just concatenates and prints values as-is, printf() lets you control exactly how each value appears — its width, number of decimals, padding, sign, and number base — by inserting format specifiers (placeholders such as %s or %.2f) into a format string. This is the right tool whenever the layout of the output matters: aligned tables, money with two decimals, zero-padded IDs, or hex color codes.
This page covers the syntax, the anatomy of a format specifier, the most common conversions with runnable examples, and the closely related functions in the same family.
Syntax
printf(string $format, mixed ...$values): int$format— a template string containing literal text plus one or more format specifiers.$values— the values substituted into the specifiers, in order.- Returns the number of characters written (the length of the output), not the string itself.
printf()prints directly to output. If you want the formatted string returned instead of printed, usesprintf().
A basic example
%s is replaced by the first argument ($name) formatted as a string, and %d by the second ($age) formatted as a signed integer. The output is:
My name is John Doe and I am 35 years old.Anatomy of a format specifier
Every specifier begins with % and may include optional parts in this order:
%[argnum$][flags][width][.precision]specifier- flags — e.g.
0(pad with zeros),-(left-justify),+(always show sign),' '(a space pads with that character). - width — the minimum number of characters the field occupies.
- .precision — for floats, the number of digits after the decimal point; for strings, the maximum length.
- specifier — the conversion type (see below).
To output a literal percent sign, escape it as %%.
Common conversion specifiers
| Specifier | Converts the argument to |
|---|---|
%s | a string |
%d | a signed decimal integer |
%f | a floating-point number |
%b | binary |
%o | octal |
%x / %X | lowercase / uppercase hexadecimal |
%e | scientific notation |
%% | a literal % |
Width, precision, and padding
This is where printf() earns its keep. The example below formats a price to two decimals, zero-pads an ID to 5 digits, and renders a number as a hex color:
<?php
$price = 7.5;
$id = 42;
$color = 16711680; // red
printf("Price: $%.2f\n", $price);
printf("Order ID: %05d\n", $id);
printf("Color: #%06X\n", $color);
?>Output:
Price: $7.50
Order ID: 00042
Color: #FF0000%.2f forces exactly two decimals, %05d pads the integer with zeros to a width of 5, and %06X produces uppercase hex padded to 6 digits.
Argument swapping
A specifier can name which argument it uses with n$, so you can reuse a value or reorder arguments without changing the list — handy for translatable strings where word order differs by language:
<?php
printf('%1$s likes %2$s. %2$s likes %1$s too.', 'Ann', 'tea');
?>Output:
Ann likes tea. tea likes Ann too.The printf family
printf() belongs to a family of functions that share the exact same format syntax but differ in what they do with the result:
sprintf()— returns the formatted string instead of printing it. Use it when you need to store or further process the result.vprintf()— likeprintf(), but takes the values as a single array (vprintf($format, $args)), useful when the arguments are already in an array.fprintf()— writes the formatted string to a stream/file handle rather than standard output.
For locale-aware number formatting (thousands separators, currency), see number_format() instead.
When to use it
Reach for printf() (or sprintf()) when output layout matters: aligned columns, fixed-decimal money, zero-padded identifiers, or generating strings like hex colors and URLs. For plain output where formatting is irrelevant, echo is simpler and faster.