PHP ob_get_flush() Function

What is PHP ob_get_flush() Function?

If you want to – 1) flush the output buffer, 2) get the content as a string, 3) and then, turn off the output buffering, use ob_get_flush() function. After executing this function, the output buffering layer no longer exists.

Syntax:

ob_get_flush()

Parameters:

The function has no parameter.

Return Values:

The function returns-

  • TRUE on success and
  • FALSE on failure (Reason could be no active buffer)

Examples:

<?php
   ob_start();
   echo "Before ob_get_flush(), Output Buffer level is: " . ob_get_level() . "<br />";
   $ob_content = ob_get_flush();
   echo "<u>The content that ob_get_flush() sent to the string is:</u> " . "<br />" . $ob_content . "<br />";
   echo "After ob_get_flush(), Output Buffer level is: " . ob_get_level() . "<br />";
?>

Output:

Before ob_get_flush(), Output Buffer level is: 2
The content that ob_get_flush() sent to the string is:
Before ob_get_flush(), Output Buffer level is: 2
After ob_get_flush(), Output Buffer level is: 1

Explanation:

After declaring the ob_start() function in line #2, the 2nd output buffering level starts. Note, the 1st output buffering level starts in the php.ini file by default.

In line #4, the ob_get_flush() function flush the content and save it to the $ob_content string.

As the ob_get_flush() function destroyed the 2nd output buffering level in line #4, so the output buffering level at line #6 is 1.

PHP Version Support:

PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8

Summary: PHP ob_get_flush() Function

The ob_get_flush() function is a built-in PHP function and part of the PHP’s Output Control functions. Remember, when you need to retrieve the content of the active buffer, you can use this function.

Reference:

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