vsprintf
Formats a string by a template, taking the values as an array.
Syntax
vsprintf(string $format, array $values): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $format | string | A template with `%s`, `%d`, `%.2f` markers. |
| $values | array | The values to insert. |
Return value
stringThe formatted string.
Examples
Values from an array:
<?php
$row = ['Tea', 2, 4.5];
echo vsprintf('%s x%d = $%.2f', $row);Output:
Tea x2 = $4.50Notes
sprintf($format, ...$values) with the spread operator does the same.
See also
Related recipes
In the tutorial
- Output and formatting — How to print data in PHP — echo and print, multi-line heredoc text, printf and sprintf templates, number formatting and debug output.