ob_get_clean
Returns the contents of the active output buffer as a string and turns the buffer off.
ob_get_clean(): string|false- Added in PHP 4.3.0
Return value
string|falseReturns the contents of the active output buffer on success, or false on failure.
Errors
Fails and emits an E_NOTICE when the active buffer was started without the PHP_OUTPUT_HANDLER_REMOVABLE flag; with no active buffer at all it returns false without a notice.
ob_get_clean() is the workhorse of output buffering: it hands you everything written since ob_start() as a string, discards the buffer, and closes it. Nothing is printed. It calls the handler with the PHP_OUTPUT_HANDLER_CLEAN and PHP_OUTPUT_HANDLER_FINAL flags and ignores the handler's return value.
When to reach for it
This is how you turn code that echos into code that returns a string. Classic uses: rendering a plain PHP template file into a variable so it can be cached, emailed, or embedded; capturing the output of var_dump(), print_r($x, false) or debug_zval_dump() for a log; and grabbing the text a third-party function prints instead of returns.
What trips people up
The return type is string|false, and an empty buffer legitimately returns "". Both are falsy, so if (!$out) cannot tell "the template rendered nothing" from "there was no buffer". Compare with === false.
The other classic bug is an early return, throw or exit between ob_start() and ob_get_clean(): the buffer stays open and later output is silently swallowed. Put the capture in a finally block, or check ob_get_level() afterwards.
Example
When to use it
- Render a plain PHP template file into a string so it can be cached, emailed, or injected into a layout.
- Capture var_dump() or print_r() output into a log line instead of printing it.
- Convert a legacy function that echoes HTML into one that returns it.
Frequently asked questions
What is the difference between ob_get_clean and ob_get_contents?
ob_get_contents() returns the buffer and leaves it open with its contents intact; ob_get_clean() returns it, empties it, and closes the buffer.
Does ob_get_clean print anything?
No. It discards the buffer instead of sending it, so the captured text appears only if you echo the returned string yourself. Use ob_get_flush() if you want both.
Why does ob_get_clean return false?
There was no active output buffer, or the buffer was started without the PHP_OUTPUT_HANDLER_REMOVABLE flag. An empty buffer returns an empty string, not false.
More examples
Rendering a template to a string
The heading and both list items are returned as one string rather than printed by the function, so the caller decides what to do with them. The byte count confirms the whole fragment was captured.
<?php
function render(string $title, array $items): string {
ob_start();
echo "<h1>$title</h1>\n";
foreach ($items as $item) {
echo "<li>$item</li>\n";
}
return ob_get_clean();
}
$page = render('Cart', ['Mug', 'Shirt']);
echo $page;
echo "-- ", strlen($page), " bytes rendered --\n";Capturing var_dump output
var_dump() writes straight to output and has no string form, so buffering is the only way to get it into a variable. The count reads 6 — one line per dumped element plus the wrapping braces.
<?php
$data = ['id' => 7, 'ok' => true];
ob_start();
var_dump($data);
$dump = ob_get_clean();
echo "captured ", substr_count($dump, "\n"), " lines\n";
echo $dump;
Discussion