What is the usleep method in PHP?
The usleep method can be used to make the program execution
1000000micro second =1second
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
<?phpecho "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 for3seconds. -
We printed the time. This time will be
3seconds after the first printed time.
Points to be noted
-
Some Operating systems do not support
usleepif the sleep time is more than .1second1000000 microseconds -
If the sleep time requirement comes in seconds, then use the
sleep(seconds)method instead of theusleep(microseconds)method. -
The sleeping time may be longer on the
WindowsOperating system than the provided microseconds.