PHP sprintf() Function

Return a formatted string

Try sprintf() - edit and run
Output
Alice bought 3 items for $47.50
00042
ff FF 10 101

What does sprintf() do?

sprintf() builds a string from a format template and values. Each % conversion specification (such as %s, %d or %.2f) is replaced by the next argument. printf() takes the same arguments but prints the result instead of returning it.

Syntax

sprintf(string $format, mixed ...$values): string

Parameters

  • $format - The template. A specification is %[argnum$][flags][width][.precision]specifier. Common specifiers: s string, d integer, f float, x/X hex, o octal, b binary, e scientific, %% a literal percent sign.
  • ...$values - Values to insert, in order (or by position with %1$s, %2$s ...).

Return value

The formatted string. Since PHP 8.0, passing fewer values than the format requires throws an ArgumentCountError.

sprintf() examples

Every example below shows the exact output it produces. Paste any of them into the editor above (or the online compiler) to run it.

1. Strings, integers, floats and number bases

<?php
$name = "Alice";
$items = 3;
$total = 47.5;
echo sprintf("%s bought %d items for $%.2f", $name, $items, $total) . "\n";
printf("%05d\n", 42);
printf("%x %X %o %b\n", 255, 255, 8, 5);

Output:

Alice bought 3 items for $47.50
00042
ff FF 10 101

2. Align columns with width and padding

<?php
$rows = [["Apple", 1.2], ["Watermelon", 3.75], ["Fig", 0.5]];
foreach ($rows as [$fruit, $price]) {
    printf("%-12s|%8.2f\n", $fruit, $price);
}

Output:

Apple       |    1.20
Watermelon  |    3.75
Fig         |    0.50

3. Argument swapping, percent signs and custom padding

<?php
printf("%2\$s is %1\$d years old\n", 30, "Bob");
printf("Progress: %d%%\n", 75);
printf("[%'*10s]\n", "PHP");
printf("[%+d] [%+d]\n", 5, -5);

Output:

Bob is 30 years old
Progress: 75%
[*******PHP]
[+5] [-5]

4. sprintf() vs number_format() for money

<?php
$amount = 1234567.891;
echo sprintf("%.2f", $amount) . "\n";
echo number_format($amount, 2) . "\n";
echo number_format($amount, 2, ',', '.') . "\n";

Output:

1234567.89
1,234,567.89
1.234.567,89

Common mistakes and gotchas

  • Too few arguments: throws ArgumentCountError in PHP 8 (PHP 7 only returned false with a warning).
  • %d truncates: sprintf("%d", 3.99) gives "3". Use %.0f or round() to round.
  • Double quotes and $: in double-quoted strings write positional arguments as %1\$s (or use single quotes) so PHP does not try to interpolate a variable.
  • Thousands separators: sprintf() has none; use number_format().

PHP sprintf() FAQ

What is the difference between sprintf() and printf()?

sprintf() returns the formatted string; printf() outputs it and returns its length.

How do I format a number with 2 decimal places in PHP?

Use sprintf("%.2f", $n) for a plain number or number_format($n, 2) if you also want thousands separators.

How do I add leading zeros?

Use a zero flag and width: sprintf("%05d", 42) returns "00042". str_pad($n, 5, "0", STR_PAD_LEFT) also works.