W3docs

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:

  1. Get — returns the current contents of the topmost output buffer as a string.
  2. Clean — discards that buffer and turns it off (equivalent to ob_get_contents() followed by ob_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|false

It 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 output

Output:

THIS WILL BE BUFFERED

The 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.

FunctionReturns contents?Stops buffering?Sends to next buffer/browser?
ob_get_clean()YesYesNo (discarded)
ob_get_contents()YesNoNo (buffer stays active)
ob_end_clean()NoYesNo (discarded)
ob_get_flush()YesYesYes (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. Calling ob_get_clean() without a matching ob_start() returns false and emits a notice. Guard with ob_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 "", not false. Use === false to 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().

Practice

Practice
What does the 'ob_get_clean()' function in PHP do?
What does the 'ob_get_clean()' function in PHP do?
Was this page helpful?