What is the purpose of the sleep function in PHP?
In PHP, the `sleep()` function is used to pause the execution of a script for a specific amount of time (in seconds). It can be used in situations where a delay is needed before continuing the script execution after a specified interval.
Here is the syntax for the `sleep()` function:
sleep(int $seconds): int|false
The `$seconds` parameter specifies the time (in seconds) to pause, and the return value is an integer indicating the actual time slept (if successful) or `false` if there is an error.
Here is an example of using the `sleep()` function to delay outputting “Hello, world!” by 3 seconds after the script is executed.
echo "Hello, world!";sleep(3);
echo "Delayed output after sleep!";
In the above example, `sleep(3)` means pausing the script execution for 3 seconds. Therefore, “Delayed output after sleep!” will be displayed with a 3-second delay after “Hello, world!” is output.