What is PHP ob_get_clean() function?
If you want to get everything from the current output buffer to a variable and turn off the buffering level, use ob_get_clean() function. But, the content in the other output buffers remains intact. (Check example 2).
Syntax:
ob_get_clean()
Parameters:
This function has no parameter.
Return Values:
The function returns –
- Content of the current output buffer on success and
- FALSE on inactivity of any output buffer.
Examples:
Example 1:
<?php
ob_start();
echo "This content is from second level output buffer.";
$content=ob_get_clean();
echo "Content of the removed output buffer is: ".$content;
?>
Output:
Content of the removed output buffer is: This content is from second level output buffer.
Explanation:
Though, the output buffer (started at line line #2) as well as it’s content (line #3) is removed by the ob_get_clean() function (line #4), the ob_get_clean() function also sends the entire buffer’s content to the $content variable (line #4). The $content variable at last line prints the deleted output buffer’s content.
Example 2:
<?php
ob_start();
echo"This content is from second level output buffer."."<br />";
ob_start();
echo "This content is from third level output buffer.";
$content=ob_get_clean();
echo"Content of the removed output buffer is: ".$content;
?>
Output:
This content is from second level output buffer.
Content of the removed output buffer is: This content is from third level output buffer.
Explanation:
Though the third output buffer (started at line line #4) as well as it’s content (line #5) i removed by the ob_get_clean() function (line #4), the ob_get_clean() function also sends the buffer’s content (line #5) to the $content variable (line #4). The $content variable at last line prints the deleted output buffer’s content. Note, the ob_get_clean() function have no effect on the previous output buffer’s content.
Note:
- By default, the buffer created by ob_start() function is removable. See example 1. If you don’t want PHP to remove content from the buffer, declare ob_start() function using bitwise XOR operator(^) with PHP_OUTPUT_HANDLER_REMOVABLE flags.
- Alternative of ob_get_clean() function: Combining both ob_get_contents() and ob_end_clean() accomplish the sametask as ob_get_clean() function.
PHP Version Support:
PHP 4>= 4.3.0, PHP 5, PHP 7, PHP 8
Summary: PHP ob_get_clean() Function
PHP ob_implicit_flush() function is a built-in PHP function and part of the PHP’s Output Control functions. It is a powerful and useful tool to get the entire content of the active buffer and then destroying it.