Content · Programming · PHP

PHP fflush() Function: Usage and Examples

Learn how PHP fflush works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Flushes PHP buffered output for a writable file stream.

What Is fflush() in PHP?

Flushes PHP buffered output for a writable file stream.

Its syntax is:

fflush(resource $stream): bool

Parameters:

  • $stream — Accepted as resource; required argument.

Return type: bool. fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.

Availability: Requires a valid writable stream.

Example 1: Basic Usage

<?php

$stream = tmpfile(); fwrite($stream, "abc"); var_dump(fflush($stream)); fclose($stream);

Output:

bool(true)

Fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.

Example 2: A Different Input

<?php

$stream = tmpfile(); fwrite($stream, "report"); echo fflush($stream) ? "Flushed" : "Error"; fclose($stream);

Output:

Flushed

Fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.

Practical Example: Applying It in a Program

<?php

$stream = tmpfile(); fwrite($stream, "checkpoint"); fflush($stream); rewind($stream); echo stream_get_contents($stream); fclose($stream);

Output:

checkpoint

Fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.

Important Behavior and Edge Cases

  • Key behavior: fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
  • Environment: Requires a valid writable stream.

Comparison With Related Functions

FunctionPurpose
fflush()Flushes PHP buffered output for a writable file stream.
fsync()fsync() asks to synchronize file data and metadata; fdatasync() concentrates on data and required metadata.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronizationfflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.

Key Takeaways

  • Flushes PHP buffered output for a writable file stream.
  • fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.
  • Requires a valid writable stream.

Summary: fflush() does not guarantee durable disk persistence; fsync() requests stronger synchronization.

Share this resourceSend this page to someone who may find it useful.