Ad space (banner)
๐Ÿ˜PHP Lessons
Lesson 56 / 68

Waiting for a Fixed Amount of Time (sleep)

In this lesson you will learn to pause for a set time with sleep so you can write programs that control their own timing. This is for anyone searching for "PHP sleep how to use".

sleep(seconds) pauses processing for the number of seconds you specify. Use it to space out calls to an API, or to slow a process down deliberately. Unlike JavaScript, there are no callbacks involved - it simply stops.

The sample code prints "Starting", waits one second with sleep(1), and then prints "Ran after 1 second". Change the number and watch the wait change with it.

A common stumbling block for beginners is that sleep() stops the process completely. Using it heavily on a web server can hold up other requests too, so it needs care in production.

It is also used in practice for waiting a moment before retrying, to avoid hammering a server with requests. Its main home is batch jobs and command-line tools.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run PHP directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)