What is PHP ob_end_clean() function?
If you want to remove everything from the current output buffer and turn off the buffering level, use ob_end_clean() function.
Note, the content in the other output buffers remains intact. (Check example 2).
Syntax:
ob_end_clean()
Parameters:
This function has no parameter.
Return Values:
The function returns –
- TRUE on success and
- FALSE on failure (Reasons including no active buffer or any special buffer that can’t be deleted.)
Examples:
Example 1:
<?php
ob_start();
echo "Current Output Buffer level is (second): ".ob_get_level() ."<br />";
ob_flush();
flush();
ob_end_clean();
echo "Current Output Buffer level is (third): ".ob_get_level();
?>
Output:
Current Output Buffer level is (second): 2
Current Output Buffer level is (third): 1
Explanation:
After starting the new buffer level by ob_start(), the script is in level 2 which is stated in line #3. (Remember, the first buffer level starts in php.ini file). The content of this level is flushed to the browser by line #4 and #5. Then it’s content is removed and the level is turned off by ob_end_clean() function at line #6.Now, there should be just one buffer level and we find this by the last output.
Example 2:
<?php
ob_start();
echo "Current Output Buffer level is (second): ".ob_get_level() ."<br />";
ob_start();
echo "Current Output Buffer level is (third): ".ob_get_level() ."<br />";
ob_end_clean();
echo "Current Output Buffer level is: ".ob_get_level();
?>
Output:
Current Output Buffer level is (second): 2
Current Output Buffer level is : 2
Explanation:
By ob_start() function (line #2), the 2nd buffer level starts. The content at line #3 is sent to the 2nd level’s output buffer. Though, the 3rd output level starts at line #4, it sends a string (line #5) to its output buffer, but, the content as well as this buffer level is turned off by ob_end_clean() function. Now, the script is again in 2nd output buffer level which we can find by the last echo statement.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP ob_end_clean() Function
PHP ob_end_clean() function is a built-in PHP function and part of the PHP’s Output Control functions. It is very useful when you want to turn off the current buffer level as well as remove the content stored here.