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.

Updated on: 2026-03-15T07:38:23+05:30

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements