fprintf()
The fprintf() function in PHP writes a formatted string to a stream, such as a file or php://stdout, using a list of separate arguments. Learn its syntax, format specifiers, and usage with runnable examples.
Introduction
The fprintf() function writes a formatted string to a stream — a file, php://stdout, php://stderr, or any other open stream resource — and fills its placeholders from a list of separate arguments. The f in the name stands for file: unlike printf(), which prints straight to output, fprintf() sends its result to a stream you choose.
It is the stream-writing member of the printf family. Compared with vfprintf(), the two do exactly the same job but receive their values differently: fprintf() takes each value as its own argument, while vfprintf() takes a single array. Reach for fprintf() when you have a handful of separate values to write to a file or console.
This chapter covers the syntax, the format specifiers, runnable examples, how it compares with the related functions, and common gotchas.
Syntax
fprintf(resource $stream, string $format, mixed ...$values): int| Parameter | Description |
|---|---|
$stream | An open stream resource — from fopen(), or one of php://stdout, php://stderr, etc. — where the output is written. |
$format | The format string: literal text mixed with %-prefixed format specifiers. |
$values | One or more values, passed as separate arguments, that fill the specifiers in order. |
It returns the number of characters written. On modern PHP a malformed call raises an error rather than returning false, so you rarely need to test the return value for failure.
Format specifiers
The $format string combines literal text with placeholders that begin with %. The most common specifiers are:
| Specifier | Meaning |
|---|---|
%s | String |
%d | Signed decimal integer |
%f | Floating-point number |
%b | Binary representation of an integer |
%x | Hexadecimal (lowercase) |
%% | A literal percent sign |
Between the % and the type letter you can add width, padding, and precision — for example %05d (pad an integer to 5 digits with zeros) or %.2f (two decimal places). A literal % must be written as %%.
Example: writing to standard output
Opening the php://stdout stream lets you see the result immediately, which makes fprintf() easy to try:
<?php
$out = fopen("php://stdout", "w");
fprintf($out, "Name: %s | Age: %d | Balance: %.2f\n", "John", 30, 1234.5);
fclose($out);Output:
Name: John | Age: 30 | Balance: 1234.50The three arguments fill %s, %d, and %.2f in order: the string is printed as-is, %d drops the decimal part of an integer, and %.2f formats the float to exactly two decimal places.
Example: writing to a file
The classic use case is writing formatted, aligned lines to a file — for instance a small log:
<?php
$log = fopen("app.log", "a");
fprintf($log, "[%s] %-5s %s\n", "2026-07-02", "INFO", "Server started");
fprintf($log, "[%s] %-5s %s\n", "2026-07-02", "ERROR", "Disk full");
fclose($log);
echo file_get_contents("app.log");Output:
[2026-07-02] INFO Server started
[2026-07-02] ERROR Disk full%-5s left-aligns the level in a 5-character column so the messages start at the same position regardless of whether the level is INFO or ERROR. Opening the file with mode "a" (append) adds each line without truncating what is already there. Each fprintf() call writes one line, taking its values as separate arguments.
fprintf() vs printf() vs vfprintf()
These three functions share the exact same format-string rules and differ only in where the result goes and how the values are passed:
| Function | Values | Destination |
|---|---|---|
printf() | Separate arguments | Standard output |
fprintf() | Separate arguments | A stream you supply |
vfprintf() | A single array | A stream you supply |
So fprintf() is printf() that writes to a stream instead of to output, and it is vfprintf() with separate arguments instead of an array:
// Separate arguments → written to $stream
fprintf($stream, "%s is %d", $name, $age);
// The same values as an array → written to $stream
vfprintf($stream, "%s is %d", [$name, $age]);If you only want the formatted string back instead of writing it anywhere, use sprintf(). A common fprintf() idiom in command-line scripts is writing diagnostics to the error stream, keeping them separate from normal output:
<?php
fprintf(STDERR, "Error: %s (code %d)\n", "connection refused", 111);STDERR is a predefined stream constant available to PHP CLI scripts, so you do not have to open it with fopen() first.
Common gotchas
- The stream comes first. The stream resource is the first argument; the format string is second. Passing the format first (as with
printf()) is the most common mistake when switching between the two. - The stream must be writable. Opening a file with
"r"(read mode) and passing it tofprintf()fails — use a writable mode such as"w"(truncate) or"a"(append). - It writes, it doesn't return text. The return value is the character count, not the formatted string — a frequent mix-up with
sprintf(). - Too few arguments raise an error. Supplying fewer values than the format has specifiers throws an
ArgumentCountErroron PHP 8+; extra values are simply ignored. - Escape literal percent signs as
%%, otherwise PHP tries to read the next character as a specifier.
Conclusion
fprintf() formats a string and writes it to a stream, taking its values as separate arguments. It is the tool of choice when you want precise, columnar output sent to a file or to the console — log lines, reports, or CLI diagnostics on php://stderr. For the related variants, see printf() (prints to output), sprintf() (returns a string), and vfprintf() (same destination, but takes an array of values).