printf()
printf() — Output a formatted string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
printf(string $format, mixed ...$values): int
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $format | string | An integer followed by a dollar sign $, to specify which number argument to treat in the conversion. |
| ...$values | mixed |
Return value
Returns the length of the outputted string.
Example
A runnable snippet:
$n = 43951789; $u = -43951789; $c = 65; // ASCII 65 is 'A' // notice the double %%, this prints a literal '%' character printf("%%b = '%b'\n", $n); // binary representation printf("%%c = '%c'\n", $c); // print the ascii character, same as chr() function printf("%%d = '%d'\n", $n); // standard integer representation printf("%%e = '%e'\n", $n); // scientific notation printf("%%u = '%u'\n", $n); // unsigned integer representation of a positive integer printf("%%u = '%u'\n", $u); // unsigned integer representation of a negative integer printf("%%f = '%f'\n", $n); // floating point representation printf("%%o = '%o'\n", $n); // octal representation printf("%%s = '%s'\n", $n); // string representation printf("%%x = '%x'\n", $n); // hexadecimal representation (lower-case) printf("%%X = '%X'\n", $n); // hexadecimal representation (upper-case) printf("%%+d = '%+d'\n", $n); // sign specifier on a positive integer printf("%%+d = '%+d'\n", $u); // sign specifier on a negative integer
Output:
%b = '10100111101010011010101101' %c = 'A' %d = '43951789' %e = '4.39518e+7' %u = '43951789' %u = '4251015507' %f = '43951789.000000' %o = '247523255' %s = '43951789' %x = '29ea6ad' %X = '29EA6AD' %+d = '+43951789' %+d = '-43951789'
Changelog
| Version | Change |
|---|---|
| 8.5.0 | A formatter that does not specify a precision now correctly treats it as a precision of 0, instead of incorrectly resetting the precision. |
| 8.0.0 | This function no longer returns false on failure. |
| 8.0.0 | Throws a ValueError if the number of arguments is zero; previously this function emitted an E_WARNING instead. |
| 8.0.0 | Throws a ValueError if [width] is less than zero or bigger than PHP_INT_MAX; previously this function emitted an E_WARNING instead. |
| 8.0.0 | Throws a ValueError if [precision] is less than zero or bigger than PHP_INT_MAX; previously this function emitted an E_WARNING instead. |
| 8.0.0 | Throws an ArgumentCountError when fewer arguments are given than required; previously this function emitted an E_WARNING instead. |
See also
sprintf(), fprintf(), vprintf(), vsprintf(), vfprintf(), sscanf(), fscanf(), number_format(), date(), flush()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page printf() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.