PHP Function Reference

PHP ob_get_clean() Function



The PHP ob_get_clean() function returns the contents of the current output buffer and then deletes this output buffer.

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_get_clean()

Parameters

No parameter is required.

Return Value

Returns the contents of the output buffer and end output buffering. If output buffering isn't active then false is returned.

Example: ob_get_clean() example

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

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

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

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

//getting the content of output buffers 
//and then closing it
while(ob_get_level() != 0) {
  $out = ob_get_clean();
  $out = strtoupper($out);
  echo $out;
}
?>

The output of the above code will be:

CONTENT OF FIRST OUTPUT BUFFER.
CONTENT OF SECOND OUTPUT BUFFER.
CONTENT OF THIRD OUTPUT BUFFER.

❮ PHP Output Control Reference