In PHP, ob_get_clean() returns the contents of the active output buffer, then cleans that buffer and turns it off.
It is the handy combo of "read the text" plus ob_end_clean(). Beginners use it to capture HTML or messages into a string, edit the string, and print later.
Syntax
Call ob_get_clean() with the values you need. Start with this line.
PHP
$html = ob_get_clean();
Call
What it does
ob_get_clean()
Returns the buffer string and ends the buffer, or false on failure.
ob_start(); echo "hi"; $s = ob_get_clean();
Stores "hi" in $s; buffering is off afterward.
ob_get_level()
Drops by one after a successful ob_get_clean().
On failure (no suitable active buffer), it returns false. Unlike a plain get, the buffer is gone afterward, so a second call needs a new ob_start().
Library declarations
PHP
ob_get_clean(): string|false
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
string|false
The buffer contents as a string on success, or false on failure.
PHP Examples
Example 1 — Capture Echo Into a Variable
Buffer a short message, pull it into a variable with ob_get_clean(), then print a modified version.
First ob_get_clean() returns "inner" and closes only the inner buffer.
Second ob_get_clean() returns "outer:" and closes the outer buffer; we print them together.
Frequently Asked Questions
It returns the active output buffer as a string, then cleans and turns that buffer off.
ob_get_contents() copies the text and leaves buffering on. ob_get_clean() returns the text and ends the buffer.
ob_get_clean() discards after reading (like end_clean). ob_get_flush() sends after reading (like end_flush). Both return the string and end the buffer.
false, for example when there is no active output buffer.
Only if you still have another active buffer (nesting), or after you call ob_start() again. With no buffer, the second call returns false.