PHP ob_end_clean() Function: Everything You Need to Know
As a PHP developer, you may need to clear the output buffer and turn off output buffering. The ob_end_clean() function is a built-in function in PHP that allows
When PHP runs with output buffering, everything your script echos is collected in an in-memory buffer instead of being sent straight to the browser. Sometimes you want to throw that captured output away and stop buffering entirely — for example after capturing something only to inspect it, or when an error means the half-built response should never reach the user. That is exactly what ob_end_clean() does. This page covers its syntax, return value, common use cases, and how it differs from the related ob_* functions.
What is the ob_end_clean() Function?
ob_end_clean() does two things in one call to the topmost (most recently started) output buffer:
- Discards its current contents — the buffered output is deleted, not sent.
- Turns off that buffering level — the buffer is closed and removed from the stack.
The function exists since PHP 4. The name decodes as output buffer (ob) — end — clean: "end" means close the buffer level, "clean" means throw the content away (as opposed to "flush", which sends it).
Syntax
ob_end_clean(): boolIt takes no arguments and returns a boolean.
Return Value and Error Handling
| Situation | Result |
|---|---|
| A buffer was active and closed successfully | returns true |
| No active output buffer | returns false and emits an E_NOTICE |
| The buffer cannot be deleted (e.g. handler forbids it) | returns false |
Because it warns when no buffer is active, guard the call with ob_get_level() when you are not certain one is open:
<?php
if (ob_get_level() > 0) {
ob_end_clean();
}
?>Basic Example
<?php
ob_start(); // Start buffering
echo "This will be thrown away";
ob_end_clean(); // Discard the buffer, stop buffering
echo "Only this line reaches the browser";
?>Output:
Only this line reaches the browserThe first echo never appears: ob_end_clean() deleted the buffer that held "This will be thrown away".
When Would I Use It?
- Suppressing unwanted output from a function, library, or template you do not control, so it cannot corrupt the response.
- Discarding a half-rendered page when an error or redirect happens partway through, so the user gets a clean response instead of a broken one.
- Capturing then dropping output during testing or measurement (for example timing how long a render takes without actually printing it).
<?php
ob_start();
try {
render_complex_page(); // emits lots of output
throw new RuntimeException('boom');
} catch (Throwable $e) {
ob_end_clean(); // drop the partial page
http_response_code(500);
echo "Sorry, something went wrong.";
}
?>ob_end_clean() vs. Related Functions
The ob_* family splits along two axes — clean vs. flush (discard vs. send) and end vs. get (close the buffer vs. keep it open / return a string):
| Function | Sends the buffer? | Closes the buffer? | Returns the contents? |
|---|---|---|---|
ob_end_clean() | No | Yes | No |
ob_end_flush() | Yes | Yes | No |
ob_get_clean() | No | Yes | Yes (as string) |
ob_clean() | No | No (keeps buffering) | No |
If you need the discarded text as a string, reach for ob_get_clean() instead of ob_end_clean(). To send the buffer rather than drop it, use ob_end_flush(). Every one of these requires a buffer first opened with ob_start().
Nested Buffers
Output buffers form a stack. ob_end_clean() only affects the innermost buffer; outer buffers stay intact. Each level needs its own call to be closed:
<?php
ob_start(); // level 1
echo "outer ";
ob_start(); // level 2
echo "inner";
ob_end_clean(); // drops "inner", level 1 still open
echo " world";
ob_end_flush(); // sends "outer world"
?>Output:
outer worldConclusion
ob_end_clean() discards the active output buffer's contents and closes that buffering level, returning true on success. Use it to drop output you never want to send — and reach for ob_get_clean(), ob_end_flush(), or ob_clean() when you instead need the contents, want to send them, or want to keep buffering. See the PHP output control overview for the full picture.