- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
PHP time_nanosleep() Function | Tutorial
Previous Page: PHP time_nanosleep() Function
PHP time_nanosleep() Function
The time_nanosleep() function is used to suspend execution of the current thread for a given number of seconds and nanoseconds.
Syntax
bool time_nanosleep ( int $seconds , int $nanoseconds )
Parameters
- seconds: The number of seconds to sleep.
- nanoseconds: The additional number of nanoseconds to sleep (will be added to the seconds argument).
Returns
Returns true on success or false on failure. If the function fails because the requested time is too short, it will return false and set the error code to E_WARNING.
Examples
Example 1: Basic Usage
<?php
if (!time_nanosleep(2, 500000000)) {
echo 'Failed to sleep';
} else {
echo 'Slept successfully';
}
?>
This script will pause for 2 seconds and 500 milliseconds.
Example 2: Handling Errors
<?php
if (!time_nanosleep(1, 999999999)) {
switch (error_get_last()['type']) {
case E_USER_WARNING:
echo 'Sleep failed due to an invalid time interval';
break;
default:
echo 'An unknown error occurred';
break;
}
} else {
echo 'Slept successfully';
}
?>
This script handles errors that may occur if the requested time interval is not valid.
Notes
Note: This function is available in PHP 5.1.0 and later.
```
YouTip