PHP sprintf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, sprintf() builds a string from a format template and returns it.

Placeholders like %s and %d are replaced by the values you pass. Nothing is printed until you echo the result.

Syntax

Call sprintf() with the values you need. Start with this line.

PHP
$text = sprintf("%s has %d items", $name, $count);
CallWhat it does
sprintf("%s is %d", "Tom", 30)Returns "Tom is 30".
sprintf("%.2f", 3.14159)Returns "3.14".

Common placeholders: %s string, %d integer, %f float, %x hex, %b binary, %% a literal percent sign.

Library declarations
PHP
sprintf(string $format, mixed ...$values): string

Parameters

ParameterDescription
formatRequired. The template with placeholders such as %s, %d, and %f.
valuesOptional. One or more values to put into the placeholders, in order.

Return Value

Return ValueDescription
stringThe formatted string.

PHP Examples

Example 1 — Fill in a Template

Use %s for text and %d for whole numbers.

PHP
<?php
$text = sprintf("%s has %d items", "Cart", 3);

echo $text, PHP_EOL;

How It Works

  1. Write a template. %s and %d are placeholders.
  2. Pass values in order. "Cart" fills the first, 3 the second.
  3. Get a string back. The result is stored in $text.
  4. Echo it. sprintf() never prints by itself.

Example 2 — Padding, Precision, and Number Bases

Add flags between the % and the letter to control the layout.

PHP
<?php
echo sprintf("%05.1f", 3.14159), PHP_EOL;
echo sprintf("%'*10s", "php"), PHP_EOL;
echo sprintf("%-8s|", "php"), PHP_EOL;
echo sprintf("%b", 5), PHP_EOL;

How It Works

  1. Zero padding. %05.1f means width 5, one decimal, padded with zeros.
  2. Custom pad character. '* fills the left side with stars.
  3. Left align. The - flag pads on the right instead.
  4. Binary output. %b prints 5 as 101.

Example 3 — Reuse Values with Positions

Number the placeholders to change the order or repeat a value.

PHP
<?php
echo sprintf('%2$s %1$s', "World", "Hello"), PHP_EOL;
echo sprintf('%1$s, %1$s!', "Hi"), PHP_EOL;

How It Works

  1. Use n$ after %. %2$s means the second argument.
  2. Reorder values. You can place them in any order.
  3. Repeat a value. %1$s can appear more than once.
  4. Use single quotes. They stop PHP from reading $s as a variable.

Frequently Asked Questions

sprintf() formats values into a template string and returns the result. It does not print anything on its own.
sprintf(string $format, mixed ...$values): string. The first argument is the format. The rest are the values to insert.
sprintf() returns the string so you can store it. printf() prints the string right away and returns its length.
Use %.2f. For example, sprintf("%.2f", 3.14159) returns "3.14".
Write %% in the format string. For example, sprintf("%d%%", 50) returns "50%".

Next: sscanf()

sscanf() reads values out of a string using a format template.

sscanf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful