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

來源:互聯網
上載者:User
1. PHP sleep() 函數

定義和用法

sleep() 函數延遲代碼執行若干秒。
文法sleep(seconds)

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

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

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

例子

<?phpecho date('h:i:s') . "<br />"; //暫停 10 秒sleep(10);//重新開始echo date('h:i:s');?>

輸出:

12:00:08 12:00:18


2. PHP usleep() 函數

定義和用法

usleep() 函數延遲代碼執行若干微秒。
文法usleep(microseconds)

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

無傳回值。
提示和注釋

注釋:在 PHP 5 之前,該函數無法工作於 Windows 系統上。

注釋:一微秒等於百萬分之一秒。
例子

<?phpecho date('h:i:s') . "<br />";//延遲 10 描述usleep(10000000);//再次開始echo date('h:i:s');?>

輸出:

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). On other platforms, 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.