PHP fprintf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, fprintf() writes a formatted string to an open stream, such as a file handle.

Handle and format are required. Extra arguments fill the format placeholders, like sprintf().

Syntax

Call fprintf with a stream and format. Start with this line.

PHP
fprintf($fp, "Count: %d\n", $n);
CallWhat it does
fprintf($fp, "%s\n", $line)Writes a formatted line to a file.
sprintf("%d", $n)Formats into a string instead of a stream.

fprintf returns the number of bytes written, or false on failure.

Library declarations
PHP
fprintf(resource $stream, string $format, mixed ...$values): int|false

Parameters

ParameterDescription
streamRequired. An open writable stream resource.
formatRequired. Format string with placeholders.
valuesOptional. Values that fill the format placeholders.

Return Value

Return ValueDescription
int|falseBytes written, or false on failure.

PHP Examples

Example 1 — Append a Formatted Log Line

Open a file for append, write one formatted line, then close.

PHP
<?php
$path = sys_get_temp_dir() . "/codetofun-fprintf.log";
$fp = fopen($path, "a");
if ($fp === false) {
    fwrite(STDERR, "Could not open file\n");
    exit(1);
}

$bytes = fprintf($fp, "[%s] hello %s\n", date("H:i:s"), "Ada");
fclose($fp);

echo "Wrote $bytes bytes to $path\n";

How It Works

  1. Open for append. Mode "a" adds to the end of the file.
  2. Guard fopen. Stop if the handle is false.
  3. Format and write. %s placeholders get the time and name.
  4. Close the stream. Always fclose when finished.

Example 2 — Integers and Float Precision

Mix %d and %.2f for numbers in a report line.

PHP
<?php
$fp = fopen("php://memory", "r+");
fprintf($fp, "items=%d total=%.2f\n", 3, 19.5);
rewind($fp);
echo stream_get_contents($fp);
fclose($fp);
// items=3 total=19.50

How It Works

  1. %d for integers. 3 fills the first placeholder.
  2. %.2f for money-style floats. Two digits after the decimal.
  3. Write to memory. No disk file needed for this demo.
  4. Rewind then read. Cursor moves after write; rewind before reading.

Example 3 — Build Text in php://memory

Practice fprintf without touching the filesystem — great for tests.

PHP
<?php
$fp = fopen("php://memory", "r+");
fprintf($fp, "A=%s\n", "one");
fprintf($fp, "B=%s\n", "two");
rewind($fp);
$text = stream_get_contents($fp);
fclose($fp);

echo $text;
/*
A=one
B=two
*/

How It Works

  1. Open memory stream. Mode "r+" allows read and write.
  2. Call fprintf twice. Each call appends more text.
  3. Rewind the pointer. Needed before reading from the start.
  4. Read the buffer. stream_get_contents returns the full text.

Frequently Asked Questions

fprintf() writes a formatted string to a stream (usually a file opened with fopen()). It uses the same format placeholders as sprintf() and printf(), but sends output to a stream instead of returning or echoing it.
int fprintf(resource $stream, string $format, mixed ...$values). Pass a stream from fopen(), a format string with placeholders like %s or %d, then the values to insert.
sprintf() returns the formatted string. fprintf() writes it to a stream and returns the number of bytes written. Use fprintf() for files; use sprintf() when you need the string in a variable.
An integer: how many bytes were written. On PHP 8+, invalid arguments typically throw instead of returning false.
Yes. Open php://memory with fopen('php://memory', 'r+') to write to an in-memory buffer, then read it back with rewind() and stream_get_contents().

Next: get_html_translation_table()

See the character → HTML entity map PHP uses.

get_html_translation_table() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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