PHP sleep() function, usleep() function

高洛峰
Release: 2023-03-04 07:36:02
Original
1898 people have browsed it

PHP sleep() function

Definition and usage

sleep() function delays code execution for a few seconds.
Syntax sleep(seconds)

seconds required. Pause time in seconds.
Return value

If successful, return 0, otherwise return false.
Errors/Exceptions

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

Example

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

Output:

12:00:08 12:00:18


PHP usleep() function

Definition and usage

usleep() function delays code execution for a number of microseconds.
Syntax usleep(microseconds)

microseconds required. Pause time in microseconds.
Return value

No return value.
Tips and Notes

Note: Prior to PHP 5, this function did not work on Windows systems.

Note: One microsecond is equal to one millionth of a second.
Example

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

Output:
09:23:14 09:23:24

Usage of sleep and unsleep in PHP

When you need the program to pause execution for a few seconds, you can use sleep

int sleep (int $seconds) program Pause for $seconds seconds before executing.

Returns zero on success, or FALSE on error. Returns zero on success, or false on error.

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.

If the call is interrupted by a signal, this function returns a non-zero value. On Windows platforms, this value is always 192 (this value is the constant value of WAIT_IO_COMPLETION in the Windows API that waits for IO to complete). The return value on other platforms is the number of seconds sleep has not been executed.

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

If the parameter is a negative value, the function generates a warning error.

When you need the program to pause execution for 1 second, you can use usleep

void usleep ( int $micro_seconds )

Delays program execution for the given number of micro seconds.

usleep parameter is microseconds and has no return value.

When you need the program execution unit to be smaller (less than microseconds), you can use

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

If you want the program to pause execution to a certain point in time, you can use

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


For more articles related to PHP sleep() function and usleep() function, please pay attention to PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template