PHP ob_implicit_flush() Function: Everything You Need to Know
As a PHP developer, you may need to turn on or off implicit flushing of the output buffer. The ob_implicit_flush() function is a built-in function in PHP that
By default, PHP collects everything your script outputs in an internal buffer and sends it to the client in chunks. Implicit flushing changes that: it tells PHP to push output to the client after every output statement instead of waiting. The ob_implicit_flush() function is the built-in switch that turns this behavior on or off. This article covers its syntax, parameters, return value, real-world use cases, and the gotchas that trip people up.
What Is the ob_implicit_flush() Function?
The ob_implicit_flush() function toggles implicit flushing for the current output buffer level. When it is enabled, PHP behaves as if you called ob_flush() automatically after every echo, print, printf, or print_r — so output is handed off to the next layer immediately rather than being held back.
This is most useful for long-running scripts where you want the user to see progress as it happens — for example a deployment log, a CSV export, or a progress bar — instead of staring at a blank page until the whole script finishes.
Important: implicit flushing only affects PHP's own output buffer. Web servers like Apache, Nginx, and PHP-FPM keep their own buffers, and so does the browser. To genuinely push bytes all the way to the user you usually still need a separate
flush()call and, ideally, noob_start()buffer holding the data back.
Syntax
ob_implicit_flush(bool $enable = true): voidParameters
| Parameter | Type | Description |
|---|---|---|
$enable | bool | true turns implicit flushing on, false turns it off. Defaults to true. |
Return value
In PHP 8.0 and later the function returns void. In PHP 7.x and earlier it returned the previous implicit-flush state as a bool. Because of this change, do not rely on its return value in code that must run across versions.
How to Use the ob_implicit_flush() Function
The common pattern is to disable PHP's regular buffer (or end it), enable implicit flushing, and call flush() to defeat the server buffer:
<?php
// Turn off PHP's output buffering for this script so nothing is held back.
while (ob_get_level() > 0) {
ob_end_flush();
}
ob_implicit_flush(true);
for ($i = 1; $i <= 3; $i++) {
echo "Step {$i} done\n";
flush(); // defeat the web server buffer
sleep(1); // simulate slow work
}
echo "All steps finished\n";Each line appears in the browser roughly one second apart instead of all at once at the end. ob_implicit_flush(true) removes the need to call ob_flush() after every echo, while flush() pushes the data past the web server's own buffer.
On a command-line run the buffering layers above PHP don't exist, so the output already streams line by line — that makes the CLI a good place to confirm your logic before deploying behind a web server.
When Should You Use It?
- Streaming progress output — import jobs, batch processing, or anything where a status line per item keeps the connection alive and the user informed.
- Server-Sent Events (SSE) — pushing
data:lines to the browser in real time. - Debugging buffering issues — temporarily enabling it helps you see exactly where output gets stuck.
You generally do not want implicit flushing for normal pages: sending output in one batch is more efficient and lets PHP set headers later in the script.
Common Gotchas
- It is not enough on its own. Server-level buffering (and
gzip/mod_deflate) can still hold your output. For real-time delivery you may need to disable compression and callflush(). - An active
ob_start()buffer overrides it. If a buffer is open, output goes there first. End it withob_end_flush()orob_end_clean()before relying on implicit flushing. - You can't send headers after flushing. Once any output is flushed,
header()calls will fail with "headers already sent."
Related Functions
ob_start()— start a new output buffer.ob_flush()— flush the current buffer manually.flush()— flush the system write buffers below PHP.- PHP Output Control overview — the whole
ob_*family in one place.
Conclusion
The ob_implicit_flush() function provides a simple switch to make PHP send output immediately after each statement instead of buffering it. Combined with flush() and an awareness of server-side buffers, it lets you build progress indicators and streaming responses for long-running scripts. For ordinary pages, leave it off and let PHP batch the output for you.