set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
flush();
while($i < 10)
{
sleep(1);
$i++;
echo $i;
flush();
}
Why doesn’t my code print out 1, then wait and print 2 then wait and print 3. Instead, it just waits 10 seconds and prints out 12345678910 all at once?
Is there a way to print it in chunks as I want?
It’s likely because of output buffering. Try adding this at the top of the file to close all the open buffers:
You can also add
ob_flush()after theflush()command in your code:(Note that you should only have to do one of them, not both, but try it)…