PHP 8 reference · String Functions
PHP vsprintf() Function
Format a string like sprintf(), but take the values from an array. Useful when the number of values varies.
PHP 8 Since 4.1
Syntax
vsprintf(string $format, array $values): string
Parameters
| Parameter | Description |
|---|---|
format |
The template with placeholders. |
values |
An array of values. |
Return value
The formatted string.
Examples
<?php
echo vsprintf("%04d-%02d-%02d", [2026, 3, 9]), "\n";
Output
2026-03-09
<?php
$row = ["Ada", 36, "London"];
echo vsprintf("%s (%d) lives in %s", $row), "\n";
Output
Ada (36) lives in London
Version history
| Version | Change |
|---|---|
| PHP 4.1 | Added |
Related
sprintf()· Return a formatted string built from a template with placeholders like %s, %d and %.2f. Format numbers, pad and align.printf()· Output a formatted string, using the same placeholders as sprintf(), and return the number of bytes printed.- PHP Strings · tutorial