Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

printf

Prints a string formatted by a template.

Syntax

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

Parameters

ParameterTypeDescription
$formatstringThe template: `%s` is a string, `%d` an integer, `%.2f` a float with two decimals.
...$valuesmixedThe values to insert.

Return value

intThe length of the printed string.

Examples

Inserting values:

<?php
 
printf('%s costs %.2f, quantity: %d', 'Tea', 250, 3);

Output:

Tea costs 250.00, quantity: 3

Width and leading zeros:

<?php
 
printf('%05d | %-8s|', 42, 'left');

Output:

00042 | left    |

Notes

sprintf() does the same but returns the string instead of printing it.

See also

  • sprintf — Returns a string built from a format template.
  • number_format — Formats a number for display: decimals and a thousands separator.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.