PHP vsprintf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, vsprintf() returns a formatted string using values from an array.

It is the array version of sprintf().

Syntax

Call vsprintf() with the values you need. Start with this line.

PHP
$text = vsprintf("%04d-%02d-%02d", [2026, 11, 11]);
CallWhat it does
vsprintf("%04d-%02d-%02d", [2026, 11, 11])Returns "2026-11-11".
vsprintf("%s is %d", ["Asha", 25])Returns "Asha is 25".

Nothing is printed. Use echo to display the returned string, or vprintf() to print directly.

Library declarations
PHP
vsprintf(string $format, array $values): string

Parameters

ParameterDescription
formatRequired. The format string with placeholders such as %s, %d, and %.2f.
valuesRequired. An array of values that fill the placeholders in order.

Return Value

Return ValueDescription
stringThe formatted string.

PHP Examples

Example 1 — Format a Date String

Zero-pad numbers from an array.

PHP
<?php
$parts = [2026, 11, 11];
$date = vsprintf("%04d-%02d-%02d", $parts);

echo $date, PHP_EOL;

How It Works

  1. Prepare the array. It holds year, month, and day.
  2. Write the format. %04d pads to four digits with zeros.
  3. Format. vsprintf() returns the new string.
  4. Print. You see 2026-11-11.

Example 2 — Too Few Values

PHP 8 throws a ValueError if the array is too short. Catch it to see the message.

PHP
<?php
try {
    echo vsprintf("%s and %s", ["one"]);
} catch (ValueError $e) {
    echo "Error: ", $e->getMessage(), PHP_EOL;
}

How It Works

  1. Two placeholders. The format needs two values.
  2. One value given. The array has only one item.
  3. ValueError. PHP 8 throws instead of printing a warning.
  4. Catch it. The catch block prints the message.

Example 3 — Use explode() Output Directly

Split a line into an array and drop it straight into a format.

PHP
<?php
$row = explode(",", "Asha,25,Chennai");

echo vsprintf("%s (%d) lives in %s", $row), PHP_EOL;

How It Works

  1. Split the row. explode() returns three items.
  2. Match placeholders. Three placeholders for three values.
  3. Convert types. %d turns the string "25" into a number.
  4. Print. You get a readable sentence.

Frequently Asked Questions

vsprintf() returns a formatted string. It takes the values to insert as an array.
string vsprintf(string $format, array $values). Both arguments are required.
sprintf() takes separate arguments. vsprintf() takes a single array, which is useful when your data is already in an array.
PHP 8 throws a ValueError. Older versions gave a warning.
No. It returns a string. Use echo to show it or vprintf() to print directly.

Next: wordwrap()

wordwrap() wraps a long string into lines of a chosen maximum width.

wordwrap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful