What is PHP flush() Function?
If you want to display current internally buffered data to browser immediately, use flush() function. Instead of sending content to the browser instantly (by echo, print etc.), PHP uses output buffering to store the data until it reaches the end of the script and then it flushes the content to the browser.
Syntax:
flush()
Parameters:
There is no parameter in this function.
Return Values:
It doesn’t return anything.
Examples:
<?php
echo str_pad("Wait 2 second... ", 4096);
flush();
sleep(2);
echo "end.";
?>
Output:
Wait 2 seconds… end.
Explanation:
When PHP interpreter reaches at line 3, the flush() function flushes out buffered data “Wait 2 seconds…” to the browser. Then, sleep() function delays the script execution for 2 seconds. Then, as the script ends with echo at line 5, it displays the “end.” to the browser.
Important notes on flush() Function:
- Some browsers don’t display thin content even you use flush() function. In this case you can use str_pad() function to increase content volume (Check example 1).
- Some versions of Microsoft IE do’t display data even you use flush() function until it gets 256 bytes of output. In this case, you can send extra whitespaces to the browser (Check example 1).
- Even after using flush() function, some servers especially win 32 don’t display the result immediately to the browser until the script terminates.
- The function won’t display the result immediately to the browsers if your Apache server uses some modules like mod_gzip which may buffer the result of their own.
- Even after sending data immediately to the browser by flush() function, browsers may buffer the data and stop displaying it. For example, Netscape buffers content until it receives end-of-line or beginning of a tag. It won’t display a table until it receives the end </table> tag.
PHP Version Support:
PHP4, PHP5,PHP7, PHP8
Summary: PHP flush() Function
The flush() function is a built-in PHP function and part of the PHP’s Output Control functions. It flushes out the buffered content immediately to the client.