sleep()
sleep() — Delay execution.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
In order to delay program execution for a fraction of a second, use usleep() as the sleep() function expects an int. For example, sleep(0.25) will pause program execution for 0 seconds.
Signature
sleep(int $seconds): int
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $seconds | int | Halt time in seconds (must be greater than or equal to 0). |
Return value
Returns zero on success. If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be 192 (the value of the WAIT_IO_COMPLETION constant within the Windows API). On other platforms, the return value will be the number of seconds left to sleep.
Example
A runnable snippet:
// current time echo date('h:i:s') . "\n"; // sleep for 10 seconds sleep(10); // wake up ! echo date('h:i:s') . "\n";
Output:
05:31:23 05:31:33
Changelog
| Version | Change |
|---|---|
| 8.0.0 | The function throws a ValueError on negative seconds; previously, an E_WARNING was raised instead, and the function returned false. |
See also
usleep(), time_nanosleep(), time_sleep_until(), set_time_limit()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page sleep() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.