What is PHP sleep() Function?
If you want to make the script delay for few seconds, not milliseconds, use PHP sleep() function. The sleep() function delays the execution of your code for a specified number of seconds.
Syntax:
sleep(seconds)
Parameters:
seconds (Required): Number of seconds to make the script delay. It must be greater than or equal to zero. Check example 1. Don’t use fraction in this parameter. The decimal part has no effect on this function, the function only considers the integer part. Check example 2.
Return Values:
The function returns-
- 0 – on success. Check example 3.
- If the call was interrupted by a signal, sleep() returns a non-zero value.
- On Windows, this value will always be
192
(the value of theWAIT_IO_COMPLETION
constant within the Windows API). - On other platforms, the return value will be the number of seconds left to sleep.
- On Windows, this value will always be
- If negative number is used as seconds, am Uncaught ValueError is thrown. Check example 4.
Examples:
Example 1:
<?php $pause_time = 2; echo "Script starts on: ". date('h:i:s') . "<br />"; sleep($pause_time); echo "It pauses for " . $pause_time . " seconds." . "<br />"; echo "And, it completes on: " . date('h:i:s') . "<br />"; ?>
Output:
Script starts on: 08:08:02
It pauses for 2 seconds.
And, it completes on: 08:08:04
Explanation:
The sleep() function delays the code execution for 2 seconds. For this, we see the time difference before (08:08:02) and after (08:08:04) the sleep() function is 2 seconds.
Example 2:
<?php echo "Script starts on: ". date('h:i:s') . "<br />"; sleep(2.99); echo "And, it completes on: " . date('h:i:s') . "<br />"; ?>
Output:
Script starts on: 08:08:04
And, it completes on: 08:08:06
Explanation:
Though the sleep() function’s arguments is 2.99, but, the sleep function delays the code execution for 2 seconds, not 2.99 seconds. The sleep() function only takes the integer part.
Example 3:
<?php echo "Script starts on: ". date('h:i:s') . "<br />"; echo sleep(1) . "<br />"; echo "And, it completes on: " . date('h:i:s') . "<br />"; ?>
Output:
Script starts on: 08:08:06
0
And, it completes on: 08:08:07
Explanation:
Line 3: The sleep() function successfully pauses the script for 1 second and if we echo this function, you’ll see 0 in the screen.
Example 4:
<?php echo "Script starts on: ". date('h:i:s') . "<br />"; sleep(-1); echo "And, it completes on: " . date('h:i:s') . "<br />"; ?>
Output:
Script starts on: 08:08:07
Fatal error: Uncaught ValueError: sleep(): Argument #1 ($seconds) must be greater than or equal to 0 in D:\xampp\htdocs\php\sleep function.php:3 Stack trace: #0 D:\xampp\htdocs\php\sleep function.php(3): sleep(-1) #1 {main} thrown in D:\xampp\htdocs\php\sleep.php on line 3
Explanation:
As the sleep() function can’t take a negative number as arguments, so it throws the ValueError error.
Example 5:
<?php echo "Statted on: ". date('h:i:s') . "<br />"; sleep(130); echo "Completed on: " . date('h:i:s') . "<br />"; ?>
Output:
Started on: 08:08:07
Fatal error: Maximum execution time of 120 seconds exceeded in D:\xampp\htdocs\php\sleep.php on line 3
Explanation:
As the value of the max_execution_time
setting inside php.ini file is 120, but, we’re trying to make the script delay for more than that time (here 130 seconds), the sleep() function throws an Fatal error.
Practical Usages of sleep() Function:
- Controlling processing time: In some actions like polling, you might want to add an interval after each shot. Use sleep() in this case like this-
- Reducing CPU overload: A script that performs heavy data processing and computation takes huge CPU resources. To reduce CPU overload, we can add delay with sleep() function like this-
- Restricting calls on API request: APIs often have restrictions on the number of requests you can make to them within a time frame. To stay within this limit, you can add delays between calls using sleep() function like this-
Notes on sleep() Function:
- If you want to make your script delay for a fraction of seconds (microseconds), use usleep() function instead of sleep() as, sleep() function counts time in integer, not in fraction number.
- If you set the time more than the seconds mentioned in
max_execution_time
setting inside php.ini file, the script will return a fatal error. Check example 5.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP sleep() Function
When you pause a script for just a few seconds, you need to choose sleep function. It is a built-in misc function in PHP.