Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is the sleep method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The sleep method can be used to suspend the execution of a program for a specific amount of time.

Syntax


sleep(int $seconds): int

Arguments

The seconds argument denotes the number of seconds the program execution needs to wait.

The value should be positive; otherwise, the function will produce E_WARNING.

Return value

This method returns 0 upon success and false upon error.


If the waiting is interrupted by a signal, then the method returns a non-zero value. This value will always be 192 for Windows. For other operating systems, this value will be the number of seconds left to sleep.

Code

<?php
echo "Time is : ".date('h:i:s'). "\n";
$val = sleep(2);
echo "Time is : ".date('h:i:s'). "\n";
echo "Sleep method returned ". $val. "\n";
?>

Explanation

In the code above:

  • We print the current time using date('h:i:s').

  • We then call the sleep(2) method to make the program execution pause for two seconds.

  • We print the time and value returned by the sleep method.

RELATED TAGS

php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring