How to use sleep() Function In PHP

The sleep() function pauses the execution of the script for a specified number of seconds. we are going to use that function for showing the time delaying between the printing the lines on the screen.

Example: This example shows the use of sleep() function for time delay.

PHP
<?php
    echo "Start\n";
    sleep(1); // Pauses script execution for 1 seconds
    echo "End\n";
?>

Output:

Start
End

How to Set Time Delay in PHP ?

To set a time delay in PHP, you have several approaches depending on your requirements. The time delay is useful for various purposes like creating a pause in script execution or scheduling tasks.

These are the following different approaches:

Table of Content

  • Using sleep() Function
  • Using usleep() Function
  • Using while loop
  • Using DateTime() Object

Similar Reads

Using sleep() Function

The sleep() function pauses the execution of the script for a specified number of seconds. we are going to use that function for showing the time delaying between the printing the lines on the screen....

Using usleep() Function

The usleep() function pauses the execution in microseconds. we are going to use that function for showing the time delaying between the printing the lines on the screen....

Using while loop

Use a while loop with a time condition to create a delay. it will first print the line that is outside of the loop then after one second it will print the line inside the loop....

Using DateTime() Object

Create a future timestamp and compare it to the current time. then create a loop that will execute after 1 second exactly....