Trusted answers to developer questions

What is the usleep method in PHP?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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.

RELATED TAGS

usleep
php
sleep
Did you find this helpful?