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');
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.
<?php
echo date('H:i:s');
sleep(15);
flush();
echo "<br>";
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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.