As you all know that modern computer programming allows us to execute multiple scripts simultaneously. This has become possible because of the powerful systems coming in these days and your computer's ability to perform multiprogramming. In this tutorial, you will learn about the sleep() function that helps delay any PHP script execution.
The Concept of sleep() Function in PHP
sleep() function is one of the important built-in functions provided by PHP version 4.0 or above to delay any script's execution process for a certain time interval. This function takes the number of seconds as its parameter and will return TRUE if it succeeds and FALSE when a failure occurs.
A non-zero value will be returned when the call gets interrupted by a signal. If you are a Windows user, your value will always appear to be 192 that is actually the value of WAIT_IO_COMPLETION. It is a constant of the Windows API. In other operating systems, this function will return the number of seconds that is left to sleep.
The syntax of using this function is:
sleep( int $seconds )
here, seconds is where you have to specify the number of seconds for which your current script will delay its execution. If the number of seconds is given in negative, then this function will throw an error. The seconds' parameter is a mandatory one, and one must specify the value for using this function.
Exceptions, Errors, and Different Scenarios Encountered by Sleep() Function
If certain circumstances arise when using this function, it can cause exceptions. Let's discuss various scenarios when these exceptions and errors pop up.
- When there is an interrupted call by a signal, the sleep()will return a non-zero value.
- When the seconds' parameter is passed with a negative value, this function usually generates an E_WARNING.
- The sleep() function will always return a NULL (in the Windows platform, before PHP 5.3.4) if that particular script has a sleep occurred, irrespective of whether the function was previously interrupted or not.
The sleep() Function Example
An example of using this sleep() function is as follows:
<?php //first let us print the current time echo date('h : i : s'); //now, let us assign a sleep time of 16 seconds sleep(16); //your script will wake up! echo date('h: i : s'); ?>
Practical Scenario to Use for PHP sleep() Function
Usually, we do not know when this function will be used, so here are some scenarios where this function would be fit to use.
- Suppose you have created a web crawler that uses the file_get_contents() function to obtain remote pages, and you do not want to bombard remote servers with too many requests in a short period of time. So in this scenario, you can create delays between consecutive requests using the PHP sleep() function.
- Let's say you are running some batch process that heavily uses a resource. But a lot of updates have to be done simultaneously, which can cause the database server to down. In this scenario, you can use the PHP sleep() function after the execution of some batch process and give the database server a chance to fulfill any pending request.