问题
I am execution loop and inside loop there is data processing function inside loop.
for($i = 0 ; $i <=680 ; $i = $i + 40)
{
$url = 'http://www.yelp.com/biz/franchino-san-francisco?start=80';
$root = yelp($url);
var_dump($root);
}
This loop takes long time to execute, and results are echoed at the end when entire loop completes.
How can I echo the result during each iteration?
Actually what happens here? does to result are stored in buffer and at the end echoed or what?
回答1:
PHP buffers the output.
If you want to output stuff to the browser immediately you can use the flush() and ob_flush() functions:
for ($i = 0; $i <= 680; $i += 40) {
$url = 'http://www.yelp.com/biz/franchino-san-francisco?start=80';
$root = yelp($url);
var_dump($root);
flush();
ob_flush();
}
回答2:
If you are executing PHP through a web-page, this would be the behaviour.
PHP is a server side language and all code will be executed before displaying the output to the client. (using a browser)
If you want to display the result within the loop, better use console / cmd (command line)
Here is something that will help you use PHP with commandline.
来源:https://stackoverflow.com/questions/23355484/php-echo-the-result-while-iterating-through-the-loop