PHP | sleep( ) Function
Last Updated :
03 Jul, 2018
Improve
The sleep() function in PHP is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds. The sleep( ) function accepts seconds as a parameter and returns TRUE on success or FALSE on failure.
If the call is interrupted by a signal, sleep() function returns a non-zero value. On Windows, this value will always be 192 whereas, on other platforms, the return value will be the number of seconds left to sleep.
Syntax:
php
Output:
php
Output:
sleep($seconds)Parameters Used: The sleep() function in PHP accepts one parameter.
- $seconds : It is a mandatory parameter which specifies the number of seconds.
- If the call is interrupted by a signal, sleep() function returns a non-zero value.
- The value of seconds passed as parameter should be non-negative else this function will generate a E_WARNING..
<?php
// displaying time
echo date('h:i:s')."\n" ;
// delaying execution of the script for 2 seconds
sleep(2);
// displaying time again
echo date('h:i:s');
?>
06:53:48 06:53:50Program 2:
<?php
// displaying time
echo date('h:i:s')."\n" ;
// using rand() function to randomly choose
// a value and delay execution of the script
sleep(rand(1, 5));
// displaying time again
echo date('h:i:s');
?>
06:53:48 06:53:52Reference: http://php.net/manual/en/function.sleep.php