PHP printf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, printf() prints a formatted string using placeholders like %s and %d.

It returns the length of the text it printed. Use sprintf() if you want the string back instead.

Syntax

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

PHP
printf("Name: %s, Age: %d" . PHP_EOL, "Ann", 25);
CallWhat it does
printf("Hi %s", "Ann")Prints Hi Ann.
printf("%05.1f", 3.14159)Prints 003.1.
printf('%2$s %1$s', "World", "Hello")Prints Hello World using numbered arguments.

Common placeholders are %s for strings, %d for integers, %f for floats, and %% for a literal percent sign.

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

Parameters

ParameterDescription
formatRequired. The format string with placeholders.
valuesOptional. One or more values that fill the placeholders in order.

Return Value

Return ValueDescription
intThe length of the printed string.

PHP Examples

Example 1 — Fill In Placeholders

Mix text with values using %s and %d.

PHP
<?php
printf("Name: %s, Age: %d" . PHP_EOL, "Ann", 25);

How It Works

  1. Write the format. %s is for text and %d is for whole numbers.
  2. Pass the values. They fill the placeholders in order.
  3. PHP builds the string. Ann goes first, then 25.
  4. It prints right away. No echo is needed.

Example 2 — Padding and Alignment

Width and flags help you line up numbers and text.

PHP
<?php
printf("[%5d] [%-5d] [%05d]" . PHP_EOL, 42, 42, 42);
printf("%05.1f|%-6s|%6s|" . PHP_EOL, 3.14159, "ab", "cd");

How It Works

  1. A number sets the width. %5d uses 5 characters.
  2. A minus sign aligns left. %-5d pads on the right.
  3. A zero pads with zeros. %05d gives 00042.
  4. Floats take a precision. %05.1f shows one decimal in 5 characters.

Example 3 — Percent Sign and Return Value

Print a real % with %%, and capture how many characters were printed.

PHP
<?php
$len = printf("Discount: %d%%" . PHP_EOL, 20);

echo $len, PHP_EOL;

How It Works

  1. Use %% for a percent sign. A single % starts a placeholder.
  2. Capture the return. $len holds the printed length.
  3. Count characters. The text plus the newline is 14.
  4. Need the string itself? Use sprintf().

Frequently Asked Questions

printf() prints a string built from a format with placeholders and a list of values. It returns the length of the output.
int printf(string $format, mixed ...$values). The format is required, the values are optional.
printf() prints the result. sprintf() returns it as a string so you can save or reuse it.
Write %% in the format string. A single % is treated as the start of a placeholder.
Use %.2f. For example, printf("%.2f", 3.14159) prints 3.14.

Next: quoted_printable_decode()

quoted_printable_decode() converts a quoted-printable string back into plain 8-bit text.

quoted_printable_decode() 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