What is PHP ob_clean() function?
If you want to remove everything from the current output buffer, but not the buffering level, use ob_clean() function.
Note, this function will only clean the current output buffer but the buffering level remains active. The function doesn’t send the content to the browser before removing it. Also, the content in the other output buffers remains intact. (Check example 2).
Syntax:
ob_clean()
Parameters:
This function has no parameter.
Return Values:
The function returns –
- TRUE on success and
- FALSE on failure.
Examples:
Example 1:
<?php
ob_start();
echo "First content";
ob_clean();
?>
Output:
Explanation:
The string “First content” was in the output buffer. But, before it goes to the browser, ob_clean() function removes the output buffer.That’s why you see nothing to the browser.
<?php
ob_start();
echo "First content";
ob_start();
echo "Second content.";
ob_clean();
?>
Output:
First content
Explanation:
The ob_start() function at line #4 adds another buffer level and the line “Second content” goes to this buffer. So, the ob_clean() function cleans this line from this buffer level. At the end, the line “First content” is displayed in the browser.
PHP Version Support:
PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8
Summary: PHP ob_clean() Function
PHP ob_implicit_flush() function is a buil-in PHP function and part of the PHP’s Output Control functions. When you need to erase the content of the active buffer but not the buffer level itself, use this function.