How to make PHP wait before executing an action or another action?

HourglassPerhaps, this question has been asked several times already. How would you be able to make PHP wait before executing an action or another action similar to CMD’s ‘sleep’ command or VBScript ‘WScript.Sleep’ function? Well, while PHP do have two such functions namely ‘sleep(n)’ where n is the number of seconds, and ‘usleep(n)’ where n is the number of microseconds, it doesn’t actually quite mimic the behavior elicited in CMD or VBScript or some other program languages. And while PHP does wait for the number of seconds specified, the sleep function executes even before other commands execute, halting the entire process. So let’s say, I want to echo or write Begin Countdown, and make PHP count down from 3, 2, 1 before saying Times Up. If I use sleep, none of those commands will run. PHP will just standby and after meeting the specified number of seconds, it will run all the commands, thereby writing all of those words all at the same time. And even if you insert sleep or usleep in between operations, all those sleep or usleep commands will simply stack up, and accumulate the number of seconds/microseconds contained in them, pausing the entire procedure base on the accumulated number of seconds/microseconds before running everything all at once. This can be problematic if we want to have one process complete first before running another one. For example, we initiated a download function, which takes several seconds to finish downloading. And once finish, we execute another process that renames the file. If everything executes at the same time, then it will result to an error because the file to rename hasn’t been downloaded yet by the time the second operation started. Sleep or usleep, instead of pausing the second operation in order to wait for the first operation to finish, it will make PHP go to sleep mode then run everything all at once, resulting to an error in these kind of scenarios.

In order to solve this problem, we first need to understand how PHP works. Remember, PHP is a server side language, so caching and backend buffers has to be taken into consideration. In order to clear the cache, we need the command clearstatcache() function. Then since we also need to flush the system write buffers, we need to call the function flush(), as well as ob_flush() for the output buffer. Doing these will make the system reset the stacked operations, but will not terminate already running ones nor will it go back to those that have already completed.

Here, I have created a function that does everything all at once.

function rest($n)
{
  clearstatcache();
  ob_flush();
  flush();
return
  sleep($n);
}

To call the function, simply insert the code somewhere in your php file, then use the custom function rest(n), where n is the number of seconds. You may also modify the function and use usleep instead of sleep to make use of microseconds instead of seconds. Microseconds btw is counted as 1 millionth of a second, so to get 1 second, you need 1000000 microseconds in usleep.

Here’s a sample code where I made use of rest custom function:

<?php
echo 'Begin countdown<br>';
rest(2);
echo '3<br>';
rest(2);
echo '2<br>';
rest(2);
echo '1<br>';
rest(3);
echo 'Times Up!';

function rest($n)
{
  clearstatcache();
  ob_flush();
  flush();
return
  sleep($n);
}

 ?>

Please do give me credit when you use my custom function and provide a link back to my blog. I would greatly appreciate it. Until then… 🙂

Follow me at:

2 Replies to “How to make PHP wait before executing an action or another action?”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.