How to make a PHP delay

Learn how to create a delay with PHP using sleep() and usleep() functions, complete with a code example.

Creating a PHP Delay

A delay using PHP is a way of creating a pause in the execution of a script. This can be useful when there is a need to wait for an action to complete, or to stop a script from running for a set amount of time. There are several ways to create delays in PHP.

Using the sleep() Function

The simplest way to create a delay in a PHP script is to use the sleep() function. This function takes a single parameter, which is the number of seconds to pause the script for. For example, to pause the script for five seconds:

sleep(5);

The sleep() function works by suspending the current thread for the specified number of seconds. This means that the script will not continue executing until the pause is over. Note that the sleep() function can take a maximum of two parameters, with the second parameter being the number of microseconds to pause the script for.

Using the usleep() Function

The usleep() function is similar to the sleep() function, but it takes a single parameter that is the number of microseconds to pause the script for. The usleep() function is useful for creating pauses that are shorter than one second. For example, to pause the script for 500 milliseconds:

usleep(500000);

The usleep() function works in the same way as the sleep() function, by suspending the current thread for the specified number of microseconds.

Using the time_nanosleep() Function

The time_nanosleep() function is similar to the sleep() and usleep() functions, but it takes two parameters. The first parameter is the number of seconds to pause the script for, and the second parameter is the number of nanoseconds to pause the script for. For example, to pause the script for five seconds and 500 milliseconds:

time_nanosleep(5, 500000);

The time_nanosleep() function works in the same way as the sleep() and usleep() functions, by suspending the current thread for the specified number of seconds and nanoseconds.

Using the usleep() and time() Functions

The usleep() and time() functions can also be used together to create a delay in a PHP script. The time() function returns the current Unix timestamp, which can be used to calculate the amount of time that has elapsed since the start of the script. The usleep() function can then be used to pause the script until the desired amount of time has elapsed. For example, to pause the script for five seconds:


$startTime = time();
while ((time() - $startTime) < 5) {
    usleep(10000);
}

In this example, the time() function is used to get the current Unix timestamp, and then the usleep() function is used to pause the script until five seconds have elapsed. The usleep() function is called every 10 milliseconds to ensure that the script does not take too long to pause.

Conclusion

Creating a delay in a PHP script can be done using the sleep(), usleep(), and time_nanosleep() functions. The sleep() and usleep() functions are the simplest to use, but they only allow for pauses that are a whole number of seconds or microseconds. The time_nanosleep() function allows for pauses that are a fraction of a second, but it is not supported on all systems. The usleep() and time() functions can also be used together to create pauses that are an exact number of seconds.

Answers (0)