PHP ob_end_flush() Function: Everything You Need to Know
As a PHP developer, you may need to flush the output buffer and turn off output buffering. The ob_end_flush() function is a built-in function in PHP that allows
When PHP captures output in a buffer instead of sending it straight to the browser, you eventually need to release that buffer. The ob_end_flush() function does exactly that: it sends the contents of the topmost output buffer to the next level (the browser, or an outer buffer) and then deletes that buffer. This page covers what the function does, when you would reach for it, how it differs from its close relatives, and the gotchas that trip people up.
What the ob_end_flush() Function Does
ob_end_flush() performs two steps on the innermost (topmost) active output buffer:
- It flushes the buffer — its contents are passed to the parent buffer, or sent to the client if this was the last buffer on the stack.
- It turns that buffer off, removing it from the buffer stack.
It returns true on success, or false if no active buffer exists (for example, you call it twice, or you never called ob_start()). When it fails it also emits an E_NOTICE.
Buffers are a stack. Each
ob_start()pushes a new buffer on top.ob_end_flush()only acts on the one currently at the top, not on every buffer at once. Check how many are open withob_get_level().
Syntax
ob_end_flush(): boolThe function takes no arguments and returns a boolean.
Basic Example
<?php
ob_start(); // start capturing output
echo "This will be buffered"; // goes into the buffer, not the screen yet
$ok = ob_end_flush(); // send the buffer out, then close it
var_dump($ok); // bool(true)Output:
This will be bufferedbool(true)ob_start() opens the buffer, the echo lands in it, and ob_end_flush() releases the text to the browser and ends buffering. The var_dump() runs after the buffer is gone, so its output is sent directly.
When Would I Use This?
Output buffering is most useful when you need to decide late what to do with output you have already generated:
- Capture, then inspect or modify — buffer a section, read it with
ob_get_contents(), optionally rewrite it, thenob_end_flush()to ship the (possibly modified) result. - Send headers after output — because nothing reaches the client while buffering is on, you can still call
header()orsetcookie()even after you haveechoed markup.ob_end_flush()releases everything once headers are settled. - Nested templating — wrap an inner buffer, flush it up into an outer buffer that does further processing.
If you instead want to keep the captured text in a variable rather than send it, use ob_get_clean(). If you want to throw the output away, use ob_end_clean().
ob_end_flush() vs Related Functions
| Function | Sends buffer out? | Keeps buffer open? | Returns the contents? |
|---|---|---|---|
ob_end_flush() | Yes | No (closes it) | No (returns bool) |
ob_get_flush() | Yes | No (closes it) | Yes (returns the string) |
ob_flush() | Yes | Yes (stays open) | No |
ob_end_clean() | No (discards) | No (closes it) | No |
A quick way to remember it: flush sends, clean discards, get_ also hands you the string, and end_ closes the buffer instead of leaving it open.
Flushing Nested Buffers
Because ob_end_flush() only closes one level, you must call it once per buffer to fully unwind the stack:
<?php
ob_start(); // level 1
echo "outer ";
ob_start(); // level 2
echo "inner";
echo ob_get_level(); // 2 — captured into level 2
ob_end_flush(); // level 2 flushes into level 1
ob_end_flush(); // level 1 flushes to the browserOutput:
outer inner2The inner echo and the level count both land in buffer 2; the first ob_end_flush() merges them into buffer 1, and the second sends the whole thing to the client.
Common Gotchas
- Calling it with no active buffer returns
falseand raises a notice. Guard withif (ob_get_level() > 0)if you are not sure a buffer is open. - It does not flush everything — one call closes one buffer. Loop until
ob_get_level()is0to unwind every level. - Do not confuse it with
flush(). The plainflush()pushes PHP/SAPI write buffers to the client but does not touch the output-buffering stack.
Conclusion
ob_end_flush() sends the current output buffer to the next level and then closes that buffer. Reach for it when you have used ob_start() to capture output and now want to release it. Remember that buffers form a stack, that the function acts on only the topmost one, and that its clean/get_ cousins offer the discard-and-return variations you may need instead.