ob_implicit_flush

Turns implicit flushing on or off so output is flushed after every block of code that produces output.

Signature
ob_implicit_flush(bool $enable = true): void
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$enableoptionalbool= truetrue to turn implicit flushing on, false to turn it off.

Return value

voidNo value is returned.

Changelog

  • PHP 8.0.0The enable parameter expects a bool; previously an int was expected.

ob_implicit_flush() makes PHP perform a flush after every block of code that produces output, so you no longer need explicit flush() calls. It is the same mechanism as the implicit_flush ini setting, switchable at runtime.

When to reach for it

Turn it on at the top of any script whose whole purpose is to emit output over time: a Server-Sent Events endpoint, a long-running worker that logs progress to stdout, or a debugging script whose output must survive a crash halfway through. It saves scattering flush() through every loop. The CLI SAPI already enables it by default, so it mainly matters under a web SAPI.

What trips people up

It works on the layer below the userland buffer stack. If an ob_start() buffer is open — including one created by output_buffering in php.ini or by your framework — output still accumulates there and implicit flushing changes nothing you can see. Close or flush that buffer first.

Printing an empty string or sending headers does not count as output and triggers no flush. And on a busy site, flushing after every statement means many small writes and many small TCP packets; leave it off for ordinary pages.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Stream a Server-Sent Events endpoint without a flush() call after every event.
  • Emit live progress from a long-running worker script so a crash still leaves the log up to that point.
  • Debug a script that dies partway through, ensuring every echo before the failure is already written out.

Frequently asked questions

What is the difference between ob_implicit_flush and flush?

flush() performs a single flush at the point you call it; ob_implicit_flush(true) makes PHP flush automatically after every block of code that produces output, so no further flush() calls are needed.

Does ob_implicit_flush disable output buffering?

No. Buffers created by ob_start() are unaffected and still hold their contents; implicit flushing only applies to the SAPI and system write buffers beneath them.

Is ob_implicit_flush already on in CLI scripts?

Yes, the CLI SAPI enables implicit flushing by default, which is why echo appears immediately there without any flush() call.

More examples

An ob_start() buffer still holds everything back

The first line only appears when ob_end_flush() runs, and $len reads 29. Implicit flushing acts on the SAPI layer, so it cannot release text sitting in a buffer created by ob_start().

Example · php
<?php
ob_implicit_flush(true);

ob_start();
echo "still buffered by ob_start()\n";
$len = ob_get_length();
ob_end_flush();

echo "the userland buffer held $len bytes despite implicit flushing\n";

Turning it back off around a bulk section

All five lines print in order. The difference is timing, not content: the first line leaves immediately while the three batched lines wait for the explicit flush(), which is how you mix streaming and bulk output in one script.

Example · php
<?php
ob_implicit_flush(true);
echo "streamed line\n";

ob_implicit_flush(false);   // batch the next part instead
for ($i = 1; $i <= 3; $i++) {
    echo "batched $i\n";
}
flush();                    // release the batch in one go

echo "done\n";

Discussion

  • Be the first to comment on this lesson.