首頁  >  文章  >  後端開發  >  php中sleep()和usleep()函數使用對比

php中sleep()和usleep()函數使用對比

伊谢尔伦
伊谢尔伦原創
2017-06-23 13:38:563403瀏覽

1. PHP sleep() 函數

定義與用法

sleep() 函數延遲程式碼執行若干秒。
語法sleep(seconds)

seconds 必要。以秒計的暫停時間。
傳回值

若成功,回傳 0,否則回傳 false。
錯誤/異常

如果指定的描述 seconds 是負數,函數將產生一個 E_WARNING。

範例

<?php
echo date(&#39;h:i:s&#39;) . "<br />"; //暂停 10 秒
sleep(10);//重新开始
echo date(&#39;h:i:s&#39;);
?>

輸出:

12:00:08 12:00:18


2. PHP usleep() 函數

定義與用法

usleep() 函數延遲程式碼執行若干微秒。
語法usleep(microseconds)

microseconds 必要。以微秒計的暫停時間。
傳回值

無回傳值。
提示與註解

註解:在 PHP 5 之前,函數無法運作於 Windows 系統上。

註:一微秒等於百萬分之一秒。
範例

<?php
echo date(&#39;h:i:s&#39;) . "<br />";
//延迟 10 描述
usleep(10000000);
//再次开始
echo date(&#39;h:i:s&#39;);
?>

輸出:

09:23:14 09:23:24

PHP中sleep和unsleep的用法

當你需要程式暫停執行幾秒可以用sleep

int sleep ( int $seconds ) 程式暫停$seconds秒後執行。

Returns zero on success, or FALSE on error.成功回傳0,錯誤回傳false。

If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be 192 (the value of the WAIT_IO_COMPLETION constant within the Windows API , the return value will be the number of seconds left to sleep.

如果呼叫被訊號中斷,則函數回傳一個非0值。在windows平台上,這個值總是192(這個值是Windows API中等待IO完成WAIT_IO_COMPLETION的常數值)。 其它平台回傳值為sleep還沒有執行的秒數。

If the specified number of seconds is negative, this function will generate a E_WARNING.

如果參數為負值,則函數生產一個警告錯誤.

#當你需要程式暫停執行1秒一下時間的時候你可以用usleep

void usleep ( int $micro_seconds )

Delays program execution for the given number of micro seconds.

#usleep參數是微秒,且無回傳值。

當你需要程式執行單位更小(小於微秒)可以用

##time_nanosleep() - Delay for a number of seconds and nanoseconds

如果你希望程式暫停執行到某個時間點,你可以用

time_sleep_until()- Make the script sleep until the specified time

以上是php中sleep()和usleep()函數使用對比的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn