4

this is my first question here on stackoverflow, I just curious.. is it possible to delay loop in PHP ? I'm trying to print each result to browser and pause the script using sleep() before it process another loop, but it's not working, here's the script that I use:

<?php
$n = 1;

while ($n < 10) {
    echo $n."<br />";
    $n++;
    sleep(1);
}
?>

PS: I'm using Firefox and Apache2 on Linux Mint.

Dwi Murdianto
  • 43
  • 1
  • 1
  • 6

2 Answers2

12

Servers usually buffer the output of a server side script until there's enough in it to output try something like this. Combination of setting output buffering off and manually flushing the buffer. Note the implcit flush line and the flush and ob_flush lines.

<?php 
@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('max_execution_time',1200);


header( 'Content-type: text/html; charset=utf-8' );


echo "Testing time out in seconds\n";
for ($i = 0; $i < 1150; $i++) {
    echo $i." -- ";

    if(sleep(1)!=0)
    {
        echo "sleep failed script terminating"; 
        break;
    }
    flush();
    ob_flush();
}

?>
Rich
  • 4,572
  • 3
  • 25
  • 31
  • Nice that something I struggled with a while ago is helping out ;) – Rich Feb 23 '13 at 02:29
  • if we don't write the code above 4 lines with ini_set its still working same? are they set by default on localhost? – Rakhi May 07 '15 at 06:50
  • @vishal the ini_set is used to force the settings to override whatever the values have been set to at server level. If you don't have the top 4 lines then the likelyhood is that the server will buffer the output and also my test script will timeout as it probably exceeds the maximum execution time allowed by the server. As the for zlib compression line, not sure if it's required or not but by virtue that output buffering is turned off, there's no buffer to compress, so logically you dont want output compression. – Rich May 08 '15 at 15:52
  • ok also this is good way to add sleep for long process to avoid execution time error and also same time show the process log to user but if we add sleep on each iteration that exceeds the time so much means at Evey iteration it sleeps.Is this good way for lengthy process loop? – Rakhi May 11 '15 at 11:46
  • @vishal, using sleep function will only extend the script timeout length on windows, on linux it has no effect . http://stackoverflow.com/questions/740954/does-sleep-time-count-for-execution-time-limit – Rich Jun 03 '15 at 17:17
  • @Rich, Exactly it takes pretty much to time to load and displays 3 iterations at a time for the first time in Ubuntu Linux. – Puneeth Feb 15 '16 at 12:05
0

PHP executes and renders its output completely before the page renders its content. So no, you can't delay the PHP loop like you want.

You could and should do this with javascript. If you need information from the server printed on delay like this, you can use AJAX to pull the information from the server as it becomes available.

Nick Pickering
  • 3,095
  • 3
  • 29
  • 50
  • It can be done server side if need be by switching off output buffering and flushing the buffer manually. Whether it's the right solution is another thing. – Rich Feb 23 '13 at 02:25