PHP Function Reference

PHP flush() Function



The PHP flush() function requests the server to send its currently buffered output to the browser. The server configuration may not always allow this to happen.

Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.

Server modules for Apache like mod_gzip may do buffering of their own that causes this function to not result in data being sent immediately to the client.

Even the browser may buffer its input before displaying it. Netscape, for example, buffers text until it receives an end-of-line or the beginning of a tag, and it won't render tables until the </table> tag of the outermost table is seen.

Some versions of Microsoft Internet Explorer will only start to display the page after they have received 256 bytes of output, so you may need to send extra whitespace before flushing to get those browsers to display the page.

Syntax

flush()

Parameters

No parameter is required.

Return Value

No value is returned.

Example: flush() example

The example below shows the usage of flush() function.

<?php
//turn on the output buffering
ob_start();

//display each line at a time 
//with a pause of 1 second
for ($i = 1; $i<=3; $i++){
  
  //some browsers will not display the content
  //if it is too short. Therefore using 
  //str_pad() to make the output long enough
  echo "Line $i content. <br>";
  echo str_pad('', 4096)."\n";   

  //using flush() to send the 
  //string to the browser
  flush();
  sleep(1);
}

echo "Done.";
ob_end_flush();
?>

The output of the above code will be:

Line 1 content.
Line 2 content.
Line 3 content.
Done.

❮ PHP Output Control Reference