Line by line output from PHP (combination of ob_flush and flush)

  • 2020-05-12 02:22:20
  • OfStack

ob_flush/flush are described in the manual as flushing the output buffer and need to be used together, so it will cause a lot of confusion...
In fact, they operate on different things. In some cases, flush doesn't do anything at all.
The ob_* series of functions is the output buffer for the operation PHP itself.
So, ob_flush is the buffer that refreshes PHP itself.
flush, strictly speaking, only works if PHP is installed as apache's Module(handler or filter). It refreshes the WebServer(apache) buffer.
Under sapi of apache module, flush will indirectly refresh the output buffer of apache by calling sapi_module's flush member function pointer to apache api: ap_rflush. Of course, as mentioned in the manual, there are some other modules of apache, which may change the result of this action..
Some of the Apache modules, such as mod_gzip, may do their own output caching, which results in the flush() function not being immediately sent to the client browser.
Even the browser caches the received content before it is displayed. For example, the Netscape browser caches the content before it receives a newline or the beginning of an html tag, and then receives it < /table > The entire table is not displayed before the tag.
Some versions of Microsoft Internet Explorer do not display the page until they have received 256 bytes, so you must send some extra space to make these browsers display the page content.
So, the order in which you use the two correctly is ob_flush, then flush,
Of course, under other sapi, it is ok not to call flush, but in order to ensure the portability of your code, it is recommended to use it together.
In IE, 256 bytes must be output before it works, as shown in the following code:
 
function execte(){ 
echo str_pad(" ", 256); 
for ($i=1;$i<10;$i++){ 
echo $i."<Br>"; 
ob_flush(); 
flush(); 
sleep(1); 
} 
} 

Related articles: