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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
format The template with placeholders.
values An array of values.

Return value

The formatted string.

Examples

Values from an arrayTry it yourself
<?php
echo vsprintf("%04d-%02d-%02d", [2026, 3, 9]), "\n";

Output

2026-03-09
Format a database rowTry it yourself
<?php
$row = ["Ada", 36, "London"];
echo vsprintf("%s (%d) lives in %s", $row), "\n";

Output

Ada (36) lives in London

Version history

VersionChange
PHP 4.1Added

Was this page helpful?