Comparison of the use of sleep() and usleep() functions in php

伊谢尔伦
Release: 2023-03-11 08:36:01
Original
3467 people have browsed it

1. 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
Copy after login


2. 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
Copy after login

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 pausing for $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 generates a warning error.

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

void usleep (int $micro_seconds)

Delays program execution for the given number of micro seconds.

usleep The parameter is microseconds and there is 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

The above is the detailed content of Comparison of the use of sleep() and usleep() functions in php. 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