PHP sleep ( ) Function
The sleep () is another PHP inbuilt function that is utilized to postpone the execution of the ongoing content for a predefined number of seconds. The sleep () function acknowledges seconds as a boundary and returns TRUE on progress or FALSE on flop cases.
On the off chance that the call is intruded on by a sign, the sleep ( ) function returns a non-zero worth. On Windows, this worth will continuously be 192 while, on different stages, the return worth will be the number of seconds left to sleep.
Syntax
Parameters The sleep ( ) function in PHP acknowledges only one parameter.
Parameter | Description | Mandatory / Optional |
---|---|---|
$ seconds | This parameter is used to define the number of seconds the program will postpone. It must be in milliseconds. | Mandatory |
The sleep () function returns TRUE on success or FALSE on a failure.
Exceptions
- If the call is interfered with by a sign, the sleep ( ) function will return a non-zero assessment.
- The worth of seconds passed as a parameter ought to be non-negative; else, this function will produce an E _ WARNING.
1. Example of sleep ( ) function
This example will help to understand sleep ( ) function completely
Output:
Example 2: Another example to help you about understanding sleep ( ) function.
Output:
Example 3: This function will allow the user to increase the time for a random interval using the sleep ( ) function
Output:
Example 4: the program to illustrate the use of multiple sleep function
Output:
PHP time ( ) function
Example: Other method to perform sleep using loop
Output:
This program shows the different ways of using the time constraint without using the sleep () function here. We have used the time () function instead. To perform the operation, we declared a variable and assigned it the time () function, which is another inbuilt PHP function, and later utilized the while loop and incremented the variable with + 4 so that it could proceed further to 4 seconds after the perfect execution of the program.
While it takes care of business, there are two issues with it. Right off the bat, time() has extremely low accuracy, just returning the number of entire seconds that have passed, which makes the entire thing very obscure. Also, PHP needs to stay there, circling a great many times while it pauses, basically sitting idle. A greatly improved arrangement is to utilize one of the two content rest capacities, sleep() and sleep(), which get some margin to stop execution as their main boundary.
Note: – The contrast between sleep() and usleep() is that sleep() requires various seconds as its boundary, while usleep() takes various microseconds – millionths of a second – as its boundary. Utilizing both of them is definitely more exact than the past time() circle, and the two of them enjoy their benefits – sleep() is better in the event that you needn’t bother with the precision, and usleep() is better, assuming you do it straight.
PHP usleep ( ) function
Syntax
Example: This function will help to understand usleep ( ) function completely
Output:
Here in this program, as you can see, the sleep () function works exactly like the sleep() function but with only one difference, sleep takes a second as a parameter, whereas the usleep () function takes a millisecond as a parameter.
Note: The default most extreme content execution time is 30 seconds, yet you can utilize sleep() and sleep() to make your contents happen for longer than that on the grounds that actually PHP doesn’t have control during the rest-activity.