vsprintf()

vsprintf() — Return a formatted string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.1.0, 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.

What it does

Operates as sprintf() but accepts an array of arguments, rather than a variable number of arguments.

Signature

vsprintf(string $format, array $values): string

Parameters

ParameterTypeWhat it takes
$formatstringAn integer followed by a dollar sign $, to specify which number argument to treat in the conversion.
$valuesarray

Return value

Return array values as a formatted string according to format.

Example

A runnable snippet:

print vsprintf("%04d-%02d-%02d", explode('-', '1988-8-1'));

Output:

1988-08-01

Changelog

VersionChange
8.5.0A formatter that does not specify a precision now correctly treats it as a precision of 0, instead of incorrectly resetting the precision.
8.0.0This function no longer returns false on failure.
8.0.0Throws a ValueError if the number of arguments is zero; previously this function emitted an E_WARNING instead.
8.0.0Throws a ValueError if [width] is less than zero or bigger than PHP_INT_MAX; previously this function emitted an E_WARNING instead.
8.0.0Throw a ValueError if [precision] is less than zero or bigger than PHP_INT_MAX; previously this function emitted an E_WARNING instead.
8.0.0Throws a ValueError when fewer arguments are given than required; previously this function emitted an E_WARNING instead.

See also

printf(), sprintf(), fprintf(), vprintf(), vfprintf(), sscanf(), fscanf(), number_format(), date()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page vsprintf() (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.