user5405873 user5405873 - 29 days ago 7
PHP Question

is there any way to sleep on each interval specified in php

i want introduce some sleep functionality into my code so that i can call a function at a specified(time or interval).

my desired output:

(1) at first 5

sec


im called


(2) at 10
sec
(i,e 5sec + 5 sec)

im called // at first iteration of loop



im called // at second iteration of loop


right now i'm using

function curl_grab(){
echo "im called<br/>";
}

$arr = ['http://ab.com/','http://bc.com/'];

foreach($arr as $el){
curl_grab($el);
sleep(5);
}


but the problem with the above code is it is dumping everything at a time

like so

im called
im called


i want it one by one

here is demo: http://phpio.net/s/ggr

Ima Ima
Answer

If you are using a browser to test this code, you will be able to see the output together only, becoz you will see output after execution of php code is completed only. You can execute this in terminal to see the out put when it happens.

Write this code into test.php

function curl_grab(){
  echo "im called\n";
}

$arr = ['http://ab.com/','http://bc.com/'];

foreach($arr as $el){
   curl_grab($el);
   sleep(5);
}

To execute in terminal you can type

$ php /path/to/test.php