PHP vprintf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, vprintf() prints a formatted string using values from an array.

It works like printf(), but takes an array instead of separate arguments.

Syntax

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

PHP
vprintf("%s has %d items" . PHP_EOL, ["Cart", 3]);
CallWhat it does
vprintf("%s has %d items", ["Cart", 3])Prints "Cart has 3 items".
vprintf("%-8s|%6.2f", ["Tea", 4.5])Prints a padded name and a price.
vprintf('%2$s %1$s', ["World", "Hello"])Uses argument numbers to reorder values.

It returns the length of the printed string. Use vsprintf() if you want the text returned instead of printed.

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

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
intThe length of the printed string.

PHP Examples

Example 1 — Print From an Array

Fill the placeholders with an array of values.

PHP
<?php
$data = ["Cart", 3];

vprintf("%s has %d items" . PHP_EOL, $data);

How It Works

  1. Prepare the values. An array holds the text and the number.
  2. Write the format. %s takes text and %d takes an integer.
  3. Print. vprintf() fills the placeholders in order.
  4. New line. PHP_EOL ends the line.

Example 2 — Padding and Precision

Format specifiers can left-align text and control decimals.

PHP
<?php
vprintf("%-8s|%6.2f" . PHP_EOL, ["Tea", 4.5]);

How It Works

  1. Left-align text. %-8s pads "Tea" to 8 characters on the right.
  2. Add a divider. The | is printed as it is.
  3. Format the price. %6.2f uses width 6 and two decimals.
  4. Result. You see 4.50 after the bar.

Example 3 — Reorder Values and Read the Length

Argument numbers like %2$s pick values by position. The function also returns the printed length.

PHP
<?php
$n = vprintf('%2$s %1$s', ["World", "Hello"]);

echo PHP_EOL, "Length: ", $n, PHP_EOL;

How It Works

  1. Use positions. %2$s means the second array item.
  2. Reorder. "Hello" prints before "World" even though it is second in the array.
  3. Store the length. The return value is 11.
  4. Print a label. We add our own line break before the label.

Frequently Asked Questions

vprintf() outputs a formatted string. The values for the placeholders come from an array.
int vprintf(string $format, array $values). Both arguments are required.
printf() takes values as separate arguments. vprintf() takes one array of values.
The length of the output string in bytes.
Use vsprintf(). It returns the formatted string.

Next: vsprintf()

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

vsprintf() 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