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
Syntax
printf(string $format, mixed ...$values): int
Parameters
| Parameter | Description |
|---|---|
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
<?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
<?php
$bytes = printf("%'.10d\n", 42); // pad with dots
echo $bytes, "\n";
Output
........42 11
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
print()· Python
Related
sprintf()· Return a formatted string built from a template with placeholders like %s, %d and %.2f. Format numbers, pad and align.vsprintf()· Format a string like sprintf(), but take the values from an array. Useful when the number of values varies.- PHP echo and print · tutorial