ob_flush

Sends the current contents of the active output buffer onward and leaves the buffer open.

Signature
ob_flush(): bool
  • Added in PHP 4.2.0

Return value

boolReturns true on success or false on failure.

Errors

Fails and emits an E_NOTICE when there is no active output buffer, or when the active buffer was started without the PHP_OUTPUT_HANDLER_FLUSHABLE flag.

ob_flush() calls the active buffer's handler with the PHP_OUTPUT_HANDLER_FLUSH flag, sends the handler's return value one level down, and empties the buffer — but the buffer stays open and keeps capturing anything you write next. The nesting level is unchanged.

When to reach for it

This is the function for streaming. Any script that emits output over time while an output buffer is open — progress lines from a batch job, a Server-Sent Events stream, a long CSV or NDJSON export — needs a periodic ob_flush() so the buffer does not simply grow until the script ends. Flushing on a size threshold also keeps memory flat on very large responses.

What trips people up

By itself ob_flush() only moves data out of PHP's userland buffer. If it was the only buffer, the text now sits in the SAPI or web server buffer instead, which can be just as invisible. The reliable pair is:

ob_flush();
flush();

The other trap is handlers. A handler that needs the whole document — gzip, HTML minification, a rewriter — is called once per flush, so flushing mid-stream produces multiple partial passes rather than one clean transformation. Do not flush a buffer whose handler is not designed for chunked operation.

Example

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

When to use it

  • Emit progress lines from a long import so the reader sees activity instead of a stalled request.
  • Stream Server-Sent Events, flushing after each event so the browser fires its handler.
  • Flush a growing export whenever ob_get_length() passes a threshold to keep memory usage flat.

Frequently asked questions

What is the difference between ob_flush and flush?

ob_flush() empties PHP's own output buffer created by ob_start(); flush() empties the SAPI and web server buffer beneath it. Streaming code normally calls both, ob_flush() first.

Does ob_flush close the output buffer?

No. It sends the current contents and empties the buffer, but the buffer stays open and ob_get_level() is unchanged. Use ob_end_flush() to send and close.

Why does ob_flush return false?

Either no output buffer is active, or the active buffer was started without the PHP_OUTPUT_HANDLER_FLUSHABLE flag — common for handlers that must see the whole document at once.

More examples

The buffer is emptied but stays open

Prints '12345' then 'length before: 5, after: 0'. The buffer is empty after the flush but still on the stack, which is exactly what distinguishes ob_flush() from ob_end_flush().

Example · php
<?php
ob_start();
echo "12345";
$before = ob_get_length();
ob_flush();
$after = ob_get_length();
$level = ob_get_level();
ob_end_flush();

echo "length before: $before, after: $after (level was $level)\n";

Flushing only when enough has accumulated

The three words print in order and the counter reads 1: the buffer only crossed 10 bytes after 'beta', so a single mid-loop flush happened and 'gamma' was released by ob_end_flush().

Example · php
<?php
ob_start();
$flushes = 0;
foreach (['alpha', 'beta', 'gamma'] as $word) {
    echo $word, "\n";
    if (ob_get_length() >= 10) {
        ob_flush();
        flush();
        $flushes++;
    }
}
ob_end_flush();
echo "flushed $flushes time(s)\n";

Discussion

  • Be the first to comment on this lesson.