PHP Function Reference

PHP ob_end_clean() Function



The PHP ob_end_clean() function deletes the contents of the topmost output buffer and turns off this output buffering.

Note: The output buffer must be started by ob_start() with PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_REMOVABLE flags. Otherwise this function will not work.

Syntax

ob_end_clean()

Parameters

No parameter is required.

Return Value

Returns true on success or false on failure.

Exceptions

Generates an E_NOTICE, if the function fails.

Example: ob_end_clean() example

The example below shows the usage of ob_end_clean() function.

<?php
//adding first output buffer
ob_start();
echo "Buffer level: ".ob_get_level()."\n";
echo "Content of first output buffer.\n";

//adding second output buffer
ob_start();
echo "Buffer level: ".ob_get_level()."\n";
echo "Content of second output buffer.\n";

//adding third output buffer
ob_start();
echo "Buffer level: ".ob_get_level()."\n";
echo "Content of third output buffer.\n";

//clearing the content of topmost output buffer
//(third output buffer) and turns it off
ob_end_clean();

//flushing and closing all output buffers
while(ob_get_level() != 0) {
  ob_end_flush();
}
?>

The output of the above code will be:

Buffer level: 1
Content of first output buffer.
Buffer level: 2
Content of second output buffer.

❮ PHP Output Control Reference