The vprintf Function

The vprintf function works similarly to printf, but accepts arguments as an array instead of a variable number of parameters. String formatting occurs according to the specified pattern.

Syntax

phpvprintf(string $format, array $args): int

Example

Basic usage with an array of arguments:

php<?php
  $format = 'Name: %s, Age: %d';
  $args = ['Ivan', 25];
  vprintf($format, $args);
?>

Code execution result:

php"Name: Ivan, Age: 25"

Example

Using various format specifiers:

php<?php
  $format = 'Price: %.2f, Code: %04d';
  $args = [19.99, 42];
  vprintf($format, $args);
?>

Code execution result:

php"Price: 19.99, Code: 0042"

Example

Usage with a variable number of arguments via call_user_func_array:

php<?php
  $format = '%s scored %d goals in %d matches';
  $data = ['Player1', 12, 15];
  call_user_func_array('vprintf', [$format, $data]);
?>

Code execution result:

php"Player1 scored 12 goals in 15 matches"

See Also

  • the printf function,
    which outputs a formatted string
  • the vsprintf function,
    which returns a formatted string
  • the sprintf function,
    which formats a string