PHP 8 reference · String Functions

PHP printf() Function

Output a formatted string, using the same placeholders as sprintf(), and return the number of bytes printed.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
format The template with %s, %d, %.2f and other placeholders.
values The values for the placeholders.

Return value

The length of the output in bytes.

Examples

Print formatted textTry it yourself
<?php
printf("%s scored %d%%\n", "Ada", 95);
printf("Pi is about %.3f\n", M_PI);

Output

Ada scored 95%
Pi is about 3.142
Custom padding and the return valueTry it yourself
<?php
$bytes = printf("%'.10d\n", 42); // pad with dots
echo $bytes, "\n";

Output

........42
11

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?