0

A bit bored and never used the sleep() function before but it doesnt really seem to do anything

All I wanted the script to do was echo from 0 to 100 with a 1 second delay in between but it just seems to hang

Not an important bit of code but its a function I've never used before so was just seeing how simple it is to use

$i = 0;
do {
   echo $i."<br>";
   sleep(1000000);
   $i++;
  } while($i < 100);
Chris Y
  • 21
  • 7
  • 1
    Possible duplicate of [PHP loop; how to print each result and delay it for a second before echoing another result?](https://stackoverflow.com/questions/15036232/php-loop-how-to-print-each-result-and-delay-it-for-a-second-before-echoing-anot) – Adam Nov 01 '18 at 16:18
  • This looks like it might help: https://stackoverflow.com/questions/15036232/php-loop-how-to-print-each-result-and-delay-it-for-a-second-before-echoing-anot I tried to run your code, with sleep(1), sleep(1000000) and using a for loop rather than do/while... couldn't get it to output anything... not tried the answer in that question though – Adam Nov 01 '18 at 16:19
  • 1
    As suresh says that, sleep takes an int in seconds, so you want sleep(1) ... even though my test didn't get that working – Adam Nov 01 '18 at 16:20
  • The modifications in that post made no difference at all – Chris Y Nov 01 '18 at 16:20
  • 1
    @CheapGamer raises a very good point in their answer below (even though imo it's far too tersely phrased). PHP scripts run on the server and generally "run to completion" before sending the complete output to the browser. So you won't see the numbers being output "in real time". (Perhaps you will if you run the script via the command line - I'm not sure if this makes any difference.) And also, a million seconds is over a week, so you wouldn't see anything anyway with that argument! – Robin Zigmond Nov 01 '18 at 16:27
  • @RobinZigmond I tried 1 second before I changed it to the 1000000 ( which I thought it took microseconds ) Ive also turned output buffering off as suggested in the other posts Normally I would have used JS but I've never used the sleep function so just wanted to see how simple it was to use – Chris Y Nov 01 '18 at 16:31
  • @RobinZigmond Also if it waited until completion I have it set to run until $i = 100 so should have outputted something by now lol – Chris Y Nov 01 '18 at 16:32

2 Answers2

0
sleep(1000000);

You're telling your program to sleep for 1 million seconds. If you intended to do milliseconds, use usleep() as sleep() uses full seconds.

-1

You should be doing this using JavaScript.

Dennishofken
  • 317
  • 2
  • 9