PHP ob_get_clean() Function: Everything You Need to Know
As a PHP developer, you may need to get the contents of the output buffer and turn off output buffering. The ob_get_clean() function is a built-in function in
The ob_get_clean() function captures everything PHP has written to the output buffer, returns it as a string, and turns the buffer off in one step. It is the standard way to grab generated output as a variable instead of sending it straight to the browser — useful for templating, caching a rendered fragment, or post-processing HTML before it is displayed.
This page covers what ob_get_clean() returns, how it differs from the related buffer functions, and the practical patterns and gotchas you'll hit when using it.
What ob_get_clean() Does
Output buffering lets PHP collect output (from echo, print, printf, even raw HTML between ?> and <?php) in memory rather than sending it immediately. You turn it on with ob_start() and then capture or release the buffered content later.
ob_get_clean() does two things at once:
- Get — returns the current contents of the topmost output buffer as a string.
- Clean — discards that buffer and turns it off (equivalent to
ob_get_contents()followed byob_end_clean()).
Because it both reads and removes the buffer, none of the captured output reaches the browser unless you echo it yourself.
Syntax
ob_get_clean(): string|falseIt takes no arguments. It returns the buffer contents as a string on success, or false if output buffering is not active (i.e. there is no buffer to read).
Basic Example
<?php
ob_start(); // start buffering
echo "This will be buffered"; // captured, not printed
$output = ob_get_clean(); // grab it and stop buffering
echo strtoupper($output); // now we control the outputOutput:
THIS WILL BE BUFFEREDThe echo "This will be buffered" line never reaches the browser on its own — it is stored in the buffer, returned into $output, and only displayed after we transform it with strtoupper().
A Practical Use: Capturing a Rendered Template
A common real-world use is rendering a template file into a string so you can return, cache, or email it:
<?php
function renderTemplate(string $file, array $data): string
{
extract($data); // turn array keys into local variables
ob_start();
include $file; // the template's HTML/echo output is buffered
return ob_get_clean(); // return it as a string
}
// Usage (assuming a greeting.php that echoes "Hello, $name!"):
// $html = renderTemplate('greeting.php', ['name' => 'Ada']);Here the included file can contain ordinary HTML and <?= $name ?> tags; ob_get_clean() turns the whole rendered result into a returnable string instead of printing it.
ob_get_clean() vs. Related Functions
| Function | Returns contents? | Stops buffering? | Sends to next buffer/browser? |
|---|---|---|---|
ob_get_clean() | Yes | Yes | No (discarded) |
ob_get_contents() | Yes | No | No (buffer stays active) |
ob_end_clean() | No | Yes | No (discarded) |
ob_get_flush() | Yes | Yes | Yes (flushed out) |
Reach for ob_get_clean() when you want the captured output and you do not want it sent anywhere automatically.
Common Gotchas
- No active buffer →
false. Callingob_get_clean()without a matchingob_start()returnsfalseand emits a notice. Guard withob_get_level()or be sure buffering is on. - It only affects the top buffer. Buffers nest. If you called
ob_start()twice,ob_get_clean()closes only the innermost one; the outer buffer is still active. - The output is gone after the call. Once cleaned, the buffer is empty — call it once and store the result in a variable if you need it more than once.
- Strict comparison for emptiness. An empty buffer returns
"", notfalse. Use=== falseto distinguish "no buffer" from "buffer was empty".
Conclusion
ob_get_clean() is the go-to function when you need the buffered output as a string and want buffering switched off afterward, without sending anything to the browser. It pairs naturally with ob_start() and powers patterns like template rendering and output caching. For the related operations — keeping the buffer open, flushing it out, or just discarding it — see ob_get_contents(), ob_get_flush(), and ob_end_clean().