Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
usleep() function in PHP
The usleep() function delays execution of the current script for a specified number of microseconds. It is useful for creating pauses in script execution or controlling timing in loops.
Syntax
usleep(microseconds)
Parameters
microseconds − The number of microseconds to delay the script. One second equals 1,000,000 microseconds.
Return Value
The usleep() function returns void (nothing).
Example
Here's how to use usleep() to create delays in script execution ?
<?php
echo date('h:i:s') . "<br>";
// wait for 2 seconds (2,000,000 microseconds)
usleep(2000000);
echo date('h:i:s') . "<br>";
// wait for 5 seconds (5,000,000 microseconds)
usleep(5000000);
echo date('h:i:s');
?>
Output
The output shows timestamps with delays between them ?
03:06:08 03:06:10 03:06:15
Microseconds vs Seconds
| Duration | Microseconds | Usage |
|---|---|---|
| 0.5 seconds | 500,000 | usleep(500000) |
| 1 second | 1,000,000 | usleep(1000000) |
| 2 seconds | 2,000,000 | usleep(2000000) |
Conclusion
The usleep() function provides microsecond-level precision for script delays. For delays longer than one second, consider using sleep() for better readability.
Advertisements
