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中文網其他相關文章!