Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to echo output as it is generated

Tags:

php

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.

like image 470
vmark99 Avatar asked Nov 26 '22 09:11

vmark99


1 Answers

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

like image 196
Mark Basmayor Avatar answered Dec 15 '22 05:12

Mark Basmayor