php sleep() function, usage of usleep() function

怪我咯
Release: 2023-03-12 22:10:01
Original
3518 people have browsed it

This article mainly introduces the PHP sleep() function, usleep() function, friends in need can refer to it

PHP sleep() function

Definition and usage

sleep() function delays code execution for a number of 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) The program will be executed after $seconds seconds.

Returns zero on success, or FALSE on error. Returns 0 on success and 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, the 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). OthersThe platform return value is the number of seconds that 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 will generate 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 To execute to a certain point in time, you can use

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

The above is the detailed content of php sleep() function, usage of usleep() function. For more information, please follow other related articles on the 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!