usleep() 函数是 PHP 标准库的内置函数,用于根据指定的要求使当前正在执行的脚本暂停几微秒和毫秒。 PHP usleep 函数没有特定的返回类型,这使得函数的时空复杂度保持不变。 PHP usleep 函数的行为类似于 sleep 函数,唯一的区别是在 PHP usleep 函数中,需要以正确的方式指定该参数,直到它作为函数的参数传递为止。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
usleep(microseconds)
语法流程是需要从函数中传递指定的参数,然后根据需求使函数可用。
从函数传递参数是一种强制方法。如上所述,没有返回类型只会使执行中的函数暂停一段时间。
usleep function() 是 PHP 中的一个内置函数,用于使整个执行过程暂停几微秒或毫秒。我们来看看 usleep 函数的工作流程如下:
以下是示例:
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.
以上是PHP 睡眠的详细内容。更多信息请关注PHP中文网其他相关文章!