What is the sleep method in PHP?

The sleep method can be used to suspend the execution of a program for a specific amount of time.

Syntax


sleep(int $seconds): int

Arguments

The seconds argument denotes the number of seconds the program execution needs to wait.

The value should be positive; otherwise, the function will produce E_WARNING.

Return value

This method returns 0 upon success and false upon error.


If the waiting is interrupted by a signal, then the method returns a non-zero value. This value will always be 192 for Windows. For other operating systems, this value will be the number of seconds left to sleep.

Code

<?php
echo "Time is : ".date('h:i:s'). "\n";
$val = sleep(2);
echo "Time is : ".date('h:i:s'). "\n";
echo "Sleep method returned ". $val. "\n";
?>

Explanation

In the code above:

  • We print the current time using date('h:i:s').

  • We then call the sleep(2) method to make the program execution pause for two seconds.

  • We print the time and value returned by the sleep method.

Free Resources