PHP | sleep( ) Function

The sleep() function in PHP is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds. The sleep( ) function accepts seconds as a parameter and returns TRUE on success or FALSE on failure.

If the call is interrupted by a signal, sleep() function returns a non-zero value. On Windows, this value will always be 192 whereas, on other platforms, the return value will be the number of seconds left to sleep.

Syntax:



sleep($seconds)

Parameters Used: The sleep() function in PHP accepts one parameter.

  • $seconds : It is a mandatory parameter which specifies the number of seconds.

Return Value: It returns TRUE on success or FALSE on failure.

Errors And Exceptions:

  1. If the call is interrupted by a signal, sleep() function returns a non-zero value.
  2. The value of seconds passed as parameter should be non-negative else this function will generate a E_WARNING..

Below programs illustrate the sleep() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// displaying time
echo date('h:i:s')."\n" ;
   
// delaying execution of the script for 2 seconds
sleep(2);
   
// displaying time again
echo date('h:i:s');
?>

chevron_right


Output:

06:53:48
06:53:50

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// displaying time
echo date('h:i:s')."\n" ;
   
// using rand() function to randomly choose
// a value and delay execution of the script
sleep(rand(1, 5));
   
// displaying time again
echo date('h:i:s');
  
?>

chevron_right


Output:

06:53:48
06:53:52

Reference: http://php.net/manual/en/function.sleep.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.