PHP Sleep

Like other languages, PHP sleep() is also used to pause program execution for a particular amount of time. This sleeping time could be controlled by a parameter, sending as an argument to this function.

PHP sleep() accepts only one and mandatory argument denoting the period of time, for what, the PHP code execution invoking this function, should be paused. This parameter should be in integer data type. And this integer value specifies the number of seconds to be passed, while putting the program execution into sleep.

php_sleep

Some languages like Javascript, not having direct functions like sleep, people might obtain sleep operation by using loop, XMLHTTP request and etc. And sometimes, people who are new to PHP may also use PHP loops for holding program execution for a while, with the required amount of time in seconds. This will be done by lot of work, like, using date / time functions for getting current time stamp and manipulating them to set limit for running loops to let program to wait for loop execution and thereby simulating sleep().

Example: PHP sleep()

Let us have a PHP example program with sleep() function pausing execution with specified amount of time.

<?php
$sleep_time = rand(5,10);
echo "<b>Putting into sleep at</b>: " . date("H:i:s") . "<br/>";
sleep($sleep_time);
echo "<b>waked up after $sleep_time seconds. Current Time is: </b>" . date("H:i:s") . "<br/>";
?>

In the above program, we have used PHP random number generator, rand() function, for randomly selecting number of seconds, the program to be put into sleep. For that, the range is specified from 5 to 10 seconds with rand().

Before invoking sleep with randomly selected period of time, we have print the time and repeat the same, after sleep is done. The program will display time in (Hour:Minute:Seconds) format, before and after sleep() function, and also, it will display how many seconds the program is paused using sleep(). And the output will be as shown below.

Putting into sleep at: 16:09:02
waked up after 5 seconds. Current Time is: 16:09:07

Related PHP Functions Pausing Execution

There are three other functions deals with the operations of pausing PHP program execution. These are differed from sleep() function with respect to the unit of time with which the parameter of these functions is passed.

These functions are listed below.

  1. usleep() – This function accepts microseconds instead of seconds received for sleep() function.
  2. time_nanosleep() – Unlike sleep() and usleep(), this function receives time parameter with the unit of seconds and nanoseconds.
  3. time_sleep_until() – this PHP function will get upcoming timestamp value with float data type for representing the time for sleep. Since, the timestamp is float, we can send this value with microseconds precision.

Caution:

The following list of point should be kept in mind on writing PHP sleep programs. These point are used to be aware of any failure or warning notice, that may cause, while invoking PHP sleep functions.

  • If we send negative values to PHP sleep functions, then it will display warning error notice to the browser, as follows.
    Warning: sleep(): Number of seconds must be greater than or equal to 0
    
  • Also sending arguments with other data type except integer value, will cause PHP warning error, like,
    Warning: sleep() expects parameter 1 to be long
    
  • While using time_sleep_until() function, if we are not specifying upcoming temporal data, rather passing old timestamp, then, this function will cause warning notice.

This PHP code tutorial was published on August 23, 2013.

↑ Back to Top