Using PHP sleep() and usleep() to Pause Execution [Examples]

Pausing PHP with sleep and usleep

This short tutorial will show you how to use the PHP sleep() and usleep() functions to pause script execution for a number of seconds/microseconds, and provide some code examples.

The PHP sleep() and usleep() functions both pause execution of the script, but both behave a bit differently.

Why Pause/Sleep PHP Script Execution?

Delaying, sleeping, or pausing PHP script execution is commonly used when scheduling an event. A call to the sleep() function might be placed at the end of a loop, so that after the code inside the loop has been run, a certain amount of time passes before the loop executes again.

The sleep() function may also be used to wait for a certain condition to be met or resource to become available. Again, a loop would be constructed which contains code checking for a response, condition, or resource to become available, with a call to the sleep() function at the end so that it will wait a few seconds before trying again.

The sleep() and usleep() functions should be used with caution to make sure you don’t cause your PHP app to freeze up for too long, especially if there’s a user at the other end waiting for content to load.

Both of the below functions can be safely used in PHP on the command line as well as in web environments.

PHP sleep() Function Syntax

The syntax for the PHP sleep() function is as follows:

sleep($SECONDS)

Note that:

  • $SECONDS should be a positive integer number of seconds the script should be paused for
  • For the duration of $SECONDS, PHP execution will be delayed – nothing will happen, no response will be output and no processing will occur
  • sleep() will return 0 (zero) on success, or false on failure
    • If sleep() is interrupted by a signal, it returns a non-zero value. On most systems this number will be the number of seconds which were remaining to sleep, but on windows it will always be the number 192

PHP usleep() Function Syntax

The syntax for the PHP usleep() function is as follows:

?usleep($MICROSECONDS)

Note that:

  • $MICROSECONDS should be a positive integer number of microseconds the script should be paused for
  • For the duration of $MICROSECONDS, PHP execution will be delayed – nothing will happen, no response will be output and no processing will occur
  • usleep() does not return a value on completion, interruption or failure!

Code Examples

sleep() Example

// Print current time
echo date('h:i:s') . "\n";

// Wait for 3 seconds
sleep(3);

// Print current time to confirm execution was paused for the specified duration
echo date('h:i:s') . "\n";

usleep() Example

// Print current time
echo date('h:i:s') . "\n";

// Wait for 3 seconds
usleep(3000000);

// Print current time to confirm execution was paused for the specified duration
echo date('h:i:s') . "\n";

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *