PHP ob_end_flush() Function

What is PHP ob_end_flush() Function?

If you want to send all the content to the client (or browser) and then turn off that output buffering, use ob_end_flush() function. After executing this function, the output buffering layer no longer exists.

Syntax:

ob_end_flush()

Parameters:

The function has no parameter.

Return Values:

The function returns-

  • TRUE on success and
  • FALSE on failure (Reasons could be no active buffer or unable to delete the buffer)

Examples:

Example 1:

<?php
ob_start();
echo "PHP"."<br />";
echo "Current Output Buffer level is: ".ob_get_level() ."<br />";
ob_end_flush();
echo "Current Output Buffer level is: ".ob_get_level();
?>

Output:

PHP
Current Output Buffer level is: 2
Current Output Buffer level is: 1

Explanation:

After declaring the ob_start() function, the second output buffering level starts. Note, the first output buffering level starts in the php.ini file by default.

Line #4 produces “Current Output Buffering level is: 2” in the buffering level. The ob_end_flush() function in line #5 flushes the content from this level 2  to level 1 and turns off this buffering level.

Now, there is only one buffering level. Line #6 produces “Current Output Buffering level is: 1” and the second ob_end_flush() function in line #5 flushes this content and turns off this buffering level.

Example 2:

<?php
ob_start();
echo "Current Output Buffering level is: ".ob_get_level() ."<br />";
ob_end_flush();
echo "Current Output Bufferinglevel is: ".ob_get_level() ."<br />";
ob_end_flush();
echo "Current Output Bufferinglevel is: ".ob_get_level() ."<br />";
ob_end_flush();
?>

Output:

Current Output Buffering level is: 2
Current Output Bufferinglevel is: 1
Current Output Bufferinglevel is: 0

Notice: ob_end_flush(): Failed to delete and flush buffer. No buffer to delete or flush in D:\xampp\htdocs\phpExercise\483. ob_end_flushfunction.php on line 8
Current Output Buffer level is: 0

Explanation:

After declaring the ob_start() function, the second output buffering level starts. Note, the first output buffering level starts in the php.ini file by default.

Line #3 produces “Current Output Buffering level is: 2” in the buffering level. The ob_end_flush() function in line #4 flushes the content from this level 2  to level 1 and turns off this buffering level.

Now, there is only one buffering level. Line #5 produces “Current Output Buffering level is: 1” and the second ob_end_flush() function in line #6 flushes this content and turn off this buffering level. Now, as there is no buffering level, the content alone with the previous content of this buffering level goes to the browser.

The echo in line #7 sends the string “Current Output Buffering level is: 0” directly the function now as there is no output buffering.

As there is no output buffering level now, the ob_end_flush() function in line #8 produces the error message in the last line.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP ob_end_flush() Function

The ob_end_flush() function is a built-in PHP function and part of the PHP’s Output Control functions. Basically, in the output buffering process, the ob_end_flush() function accomplishes the last step- sending the buffered content to the browser and then turns off the buffering layer.

Reference:

https://www.php.net/manual/en/function.ob-end-flush.php