How do I echo something for every iteration of the following loop:
<?
for($i=0;$i<5;$i++) {
// some (slow) logic
echo $i;
}
?>
I would like to see the value of $i
output as it runs, however this outputs nothing and then 0 1 2 3 4
after the script is executed.
Try this one. The key here is knowing what output_buffering value(default=4096) was set for your server.
ob_start();
$buffer = str_repeat(" ", 4096)."\r\n<span></span>\r\n";
for ($i=0; $i<25; $i++) {
echo $buffer.$i;
ob_flush();
flush();
sleep(1);
}
ob_end_flush();
http://php.net/manual/en/outcontrol.configuration.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With