usleep() function is an inbuilt function of PHP standard library which is used for making the current script in execution to a halt for some microseconds and milliseconds as per specified requirement. There is no specific return type for the PHP usleep function which makes the time-space complexity of the function constant. PHP usleep function behaves like sleep function only with a mere difference of the fact that in PHP usleep function that parameter needs to be specified in a proper way till It gets passed as a parameter from the function.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
usleep(microseconds)
The syntax flow is in a way where the specified parameter needs to be passed from the function and then the function will be made available according to the requirement.
Passing of the parameter from function is a mandatory approach. There is no return type only makes the function in execution to halt for a certain interval of time as mentioned.
usleep function() is an inbuilt function in PHP which is used to make the entire process in execution to halt for some microseconds or milliseconds. Let’s see the working flow of usleep function which is as follows :
Following are the examples are given below:
This program demonstrates the usleep() function in PHP which is used for representing the delay in execution while informing the end user of the time with the specified parameter to the function with 8 milliseconds delay as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php echo date('hr:in:sec') . "<br>"; usleep(800000); echo date('hr:in:sec'); ?> </body> </html>
Output:
This program demonstrates the difference in both the usleep() and sleep() function with the difference in CPU circle consumption. This takes input as for date in sleep() mode for 5 seconds and then start again once the halt completes for 3 seconds and behaves merely different as compared to usleep as shown in the output.
Code:
<?php echo date('h:i:s') . "\n"; sleep(5); echo date('hr:in:sec') . "\n"; ?>
Output:
This program demonstrates the difference in usleep with time_nanosecond() function containing difference with seconds and nanoseconds almost like usleep() and sleep() function as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php if (time_nanosleep(3,5000000) === true) { echo "nanosleep time for 3 or 5 seconds."; } ?> </body> </html>
Output:
This program demonstrates the difference between the PHP usleep() function and time_sleep_until() function which is used for getting the values of time in boolean format as shown in the output.
Code:
<?php var_dump(time_sleep_until(time()+1)); var_dump(time_sleep_until(microtime(false)+0.8)); ?>
Output:
Note: If the time_sleep_until() function compared to sleep function will be used then it will return value as false when given a negative value.PHP usleep() function in PHP is a function which is used for making the program in execution to halt for analyzing the fault or may be for some requirement changes but is useful for programmers as it can be handled accordingly in PHP and is a great source of manipulation with scripts in PHP.
The above is the detailed content of PHP usleep. For more information, please follow other related articles on the PHP Chinese website!