Using sleep to add delay or increase execution time

Pause execution with sleep()

sleep() pauses the current PHP process for a number of seconds. Avoid long sleeps during normal web requests because they keep the request worker occupied; they are more suitable for controlled scripts or demonstrations.

<?php
echo date('H:i:s') . PHP_EOL;
sleep(2);
echo date('H:i:s');
Delay in execution by sleep We can create a delay by using sleep command inside our PHP script. This command sleep will delay the output by seconds specified in sleep command. Here is the syntax.
sleep(25);
The above command will create a delay of 25 seconds, the next step of execution will be after the delay of 25 seconds.

Some time while testing our scripts we need to add some delay , here it is useful. Say you want to see maximum script execution time of your server. This setting you can know by increasing the sleep time till the set value of execution time at server.

Let us print the time after a delay of 15 seconds.

Here we will first display the time in hour : minute : second format and then use the php flush command to send the output to browser. Then the sleep command will delay the execution by 15 seconds and then again we will print the server time to see the delay. Here is the code.
<?php
echo date('H:i:s');
sleep(15);
flush();
echo &quot;&lt;br&gt;&quot;;
echo date('H:i:s');
?>
Out of the two outputs , second one will return after 15 secs
11:36:19
11:36:34
Introduction to PHP Adding Comments to code in PHP


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer