Skip to content
Related Articles

Related Articles

PHP | sleep( ) Function
  • Last Updated : 03 Jul, 2018

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:




<?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');
?>

Output:

06:53:48
06:53:50

Program 2:




<?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');
  
?>

Output:

06:53:48
06:53:52

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :