What is the usleep method in PHP?

The usleep method can be used to make the program execution waitsuspend for a specific microsecond().

1000000 micro second = 1 second

Syntax

usleep(int $microseconds): void

The microseconds argument denotes the number of microseconds the program execution needs to wait. This method does not return any value.

Code

<?php
echo "Time is : ".date('h:i:s'). "\n";
usleep(3000000); // sleeps for 3 seconods.
echo "Time is : ".date('h:i:s'). "\n";
?>

In the code above, we did the following:

  • We printed the current time using the date('h:i:s').

  • We then called the usleep(3000000) method to make the program execution sleep for 3 seconds.

  • We printed the time. This time will be 3 seconds after the first printed time.

Points to be noted

  • Some Operating systems do not support usleep if the sleep time is more than 1 second1000000 microseconds.

  • If the sleep time requirement comes in seconds, then use the sleep(seconds) method instead of the usleep(microseconds) method.

  • The sleeping time may be longer on the Windows Operating system than the provided microseconds.