Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

vsprintf

Formats a string by a template, taking the values as an array.

Syntax

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

Parameters

ParameterTypeDescription
$formatstringA template with `%s`, `%d`, `%.2f` markers.
$valuesarrayThe 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.50

Notes

sprintf($format, ...$values) with the spread operator does the same.

See also

  • sprintf — Returns a string built from a format template.
  • printf — Prints a string formatted by a template.
  • implode — Joins array elements into a string.

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.

Thanks for reading!

Donate to the site — we work hard for you.