PHP ob_get_flush() Function: Everything You Need to Know
As a PHP developer, you may need to get the contents of the output buffer and flush it. The ob_get_flush() function is a built-in function in PHP that allows
As a PHP developer, you may need to retrieve the contents of the output buffer and flush it at the same time. The ob_get_flush() function is a built-in PHP function that does both in a single call: it returns the current buffer contents as a string and sends them to the next output level. This article explains how it works, what it returns, and when to reach for it instead of the other output-control functions.
What Is the ob_get_flush() Function?
Output buffering lets PHP collect everything you echo or print in memory instead of sending it straight to the browser. You start a buffer with ob_start(), write to it as usual, and then decide what to do with the captured output.
ob_get_flush() does three things in one step:
- Gets the contents of the active (topmost) output buffer and returns them as a string.
- Flushes those contents — sends them on to the next buffer level, or to the browser if no other level exists.
- Closes that buffer level (one level of nesting is removed).
This makes it a convenient combination of ob_get_contents() (read) and ob_end_flush() (send + close).
Syntax
ob_get_flush(): string|falseParameters
ob_get_flush() takes no parameters.
Return Value
The function returns the contents of the topmost output buffer as a string. If output buffering is not active (there is no buffer to flush), it returns false and raises a notice. Always start a buffer with ob_start() before calling it.
How to Use the ob_get_flush() Function
The pattern is: start a buffer, write to it, then call ob_get_flush() to capture and release it.
<?php
ob_start();
echo "This will be buffered";
// Capture the buffer, flush it to the browser, and close the level.
$output = ob_get_flush();
echo "\nCaptured copy: " . $output;Output:
This will be buffered
Captured copy: This will be bufferedHere "This will be buffered" is collected in the buffer rather than printed immediately. ob_get_flush() returns that text (so $output now holds it) and at the same time sends it to the browser — that is why the phrase appears twice: once from the flush, once from the final echo.
If buffering is not active when you call it, you get false back:
<?php
$output = ob_get_flush(); // No ob_start() was called
var_dump($output); // bool(false)ob_get_flush() vs. related functions
| Function | Returns contents? | Sends to output? | Closes the buffer? |
|---|---|---|---|
ob_get_flush() | Yes (string) | Yes | Yes |
ob_end_flush() | No (bool) | Yes | Yes |
ob_get_clean() | Yes (string) | No (discarded) | Yes |
ob_get_contents() | Yes (string) | No | No |
Choose ob_get_flush() when you want to both keep a copy of the buffered output (to log, inspect, or modify) and still send it to the browser. If you only need to send it, use ob_end_flush(); if you want to grab it without sending, use ob_get_clean().
Conclusion
The ob_get_flush() function retrieves the active output buffer as a string while flushing and closing it in one call. By understanding its empty parameter list, its string|false return value, and its buffer-closing behavior, you can manage output streams more precisely. To go deeper, explore ob_start() for opening buffers, ob_get_level() for checking nesting depth, and ob_flush() for flushing without closing.