Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

sleep

Pauses the program for a number of seconds.

Syntax

sleep(int $seconds): int

Parameters

ParameterTypeDescription
$secondsintHow many seconds to wait.

Return value

int`0` on success.

Examples

Retrying with a pause:

<?php
 
// retry.php
 
for ($attempt = 1; $attempt <= 3; $attempt++) {
    echo "Attempt $attempt\n";
    sleep(1);
}

Notes

For pauses shorter than a second there is usleep(), which takes microseconds. On a site a pause delays the visitor's response, so pauses are used mostly in command-line scripts.

See also

  • hrtime — Returns a high-resolution timer reading in nanoseconds.
  • time — Returns the current timestamp.
  • microtime — Returns the current time with microsecond precision.

In the tutorial

  • Command-line scripts — PHP in the terminal — running a script, argv arguments, reading input, exit codes, getopt and running on a schedule.

Thanks for reading!

Donate to the site — we work hard for you.