php - output values to screen mid loop
Is it possible to output anything to screen during a long foreach loop?
Whenever i am performing a loop that ta开发者_C百科kes a long time, nothing is output to the screen until the loop is finished entirely - even although echo statements are inside the loop.
Is there a way to change this behaviour?
You need to flush the buffers. How to do it depends on your server set up. PHP scripts often are set up to have output buffering or even zlib buffered encoding. Anyway, flush()
and ob_flush()
are two functions that can help.
Note that none of this will help if the client itself is buffering results. i.e., There's no way to guarantee that the flushing will be effective.
You'll need to use both flush() and ob_ flush() to make sure that data is being sent. Some browsers are kind of picky and require at least 1k (1,024 bytes) of data to be sent before they will obey the flush commands.
精彩评论