printf
Prints a string formatted by a template.
Syntax
printf(string $format, mixed ...$values): intParameters
| Parameter | Type | Description |
|---|---|---|
| $format | string | The template: `%s` is a string, `%d` an integer, `%.2f` a float with two decimals. |
| ...$values | mixed | The 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: 3Width 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.