PHP sleep () function, Usleep () function _php technique

Source: Internet
Author: User
Tags constant echo date sleep usleep

PHP Sleep () function

Definitions and usage

The sleep () function delays code execution for several seconds.
Grammar Sleep (seconds)

seconds required. The pause time in seconds.
return value

If successful, returns 0, otherwise returns false.
Error/exception

If the specified description seconds is a negative number, the function generates a e_warning.

Example

<?php 
echo Date (' H:i:s '). "<br/>"; Pause 10 seconds of sleep
(10);//restart 
echo date (' h:i:s '); 
? >

Output:

12:00:08 12:00:18


PHP usleep () function

Definitions and usage

The Usleep () function delays code execution for several microseconds.
Grammar Usleep (microseconds)

microseconds required. The pause time in microseconds.
return value

no return value.
Tips and comments

Note: This function cannot work on a Windows system until PHP 5.

Note: One microsecond equals one out of 10,000 seconds.
Example

<?php 
echo Date (' H:i:s '). "<br/>"; 
Delay 10 describes
Usleep (10000000);
Start 
echo Date again (' h:i:s '); 
? >

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

Usage of sleep and unsleep in PHP

When you need a program to suspend execution for a few seconds you can sleep

the int sleep (int $seconds) program is paused $seconds seconds after execution.

Returns Zero on success, or FALSE on error. Successfully returns 0, error returns false.

If the call is interrupted by a signal, sleep () returns a Non-zero value. On Windows, this value would always be (the value of the Wait_io_completion constant within the Windows API). On the "other platforms", the return value would be the number of seconds left to sleep.

If the call is interrupted by a signal, the function returns a value other than 0. On the Windows platform, this value is always 192 (this value is the constant value of the Windows API waiting for IO to complete wait_io_completion). The other platform return value is the number of seconds that sleep has not yet been performed.

If the specified of seconds is negative, this function would generate a e_warning.

If the parameter is negative, the function produces a warning error.

When you need a program to suspend execution for 1 seconds, you can use Usleep

void Usleep (int $micro _seconds)

Delays program execution for the given number of micro seconds.

The Usleep parameter is microsecond and has no return value.

When you need a smaller program execution unit (less than microseconds) you can use the

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

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

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.