PHP vfprintf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, vfprintf() writes a formatted string to a stream, such as a file or STDOUT.

It works like fprintf(), but the values come from an array instead of separate arguments.

Syntax

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

PHP
vfprintf(STDOUT, "%s is %d" . PHP_EOL, ["Asha", 25]);
CallWhat it does
vfprintf(STDOUT, "%s is %d", ["Asha", 25])Writes "Asha is 25" to the output stream.
vfprintf($handle, "%05.1f", [3.14159])Writes "003.1" to an open file handle.

It returns the number of bytes written. In PHP 8, a values array with too few items throws a ValueError.

Library declarations
PHP
vfprintf(resource $stream, string $format, array $values): int

Parameters

ParameterDescription
streamRequired. An open stream resource, such as STDOUT or the result of fopen().
formatRequired. The format string with placeholders like %s, %d, and %f.
valuesRequired. An array of values that fill the placeholders in order.

Return Value

Return ValueDescription
intThe number of bytes written to the stream.

PHP Examples

Example 1 — Write Formatted Text to STDOUT

Pass an array of values and a format string.

PHP
<?php
vfprintf(STDOUT, "%s is %d years old" . PHP_EOL, ["Asha", 25]);

How It Works

  1. Choose a stream. STDOUT is the normal output.
  2. Write placeholders. %s is for text and %d is for whole numbers.
  3. Pass an array. Values are used in order.
  4. End the line. PHP_EOL adds a new line.

Example 2 — Use the Return Value

vfprintf() returns how many bytes it wrote. The format here pads a number with zeros.

PHP
<?php
$len = vfprintf(STDOUT, "%05.1f", [3.14159]);

echo PHP_EOL, "Written: ", $len, PHP_EOL;

How It Works

  1. Format the number. %05.1f means width 5, zero padded, one decimal.
  2. Output. 3.14159 becomes 003.1.
  3. Save the length. The function returns 5 bytes.
  4. New line. No line break was written, so we print one before the label.

Example 3 — Write to an In-Memory Stream

Use php://memory to build text in memory, then read it back.

PHP
<?php
$h = fopen("php://memory", "w+");

vfprintf($h, "%s=%s;", ["a", 1]);
vfprintf($h, "%s=%s;", ["b", 2]);

rewind($h);
echo stream_get_contents($h), PHP_EOL;
fclose($h);

How It Works

  1. Open a memory stream. php://memory acts like a temporary file.
  2. Write twice. Each call appends formatted text.
  3. Rewind. rewind() moves the pointer back to the start.
  4. Read and close. Print the content and close the stream.

Frequently Asked Questions

vfprintf() writes a formatted string to a stream. The values to insert are passed in an array.
int vfprintf(resource $stream, string $format, array $values). All three arguments are required.
fprintf() takes values as separate arguments. vfprintf() takes them as one array, which is handy when you already have an array.
It returns the number of bytes written to the stream.
PHP 8 throws a ValueError. Make sure the array has one item for each placeholder.

Next: vprintf()

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

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