PHP sleep()函数, usleep()函数

yipeiwu_com5年前PHP代码库

PHP sleep() 函数

定义和用法

sleep() 函数延迟代码执行若干秒。
语法sleep(seconds)

seconds 必需。以秒计的暂停时间。
返回值

若成功,返回 0,否则返回 false。
错误/异常

如果指定的描述 seconds 是负数,该函数将生成一个 E_WARNING。

例子

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

输出:

12:00:08 12:00:18


PHP usleep() 函数

定义和用法

usleep() 函数延迟代码执行若干微秒。
语法usleep(microseconds)

microseconds 必需。以微秒计的暂停时间。
返回值

无返回值。
提示和注释

注释:在 PHP 5 之前,该函数无法工作于 Windows 系统上。

注释:一微秒等于百万分之一秒。
例子

<?php 
echo 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

相关文章

PHP GD库添加freetype拓展的方法

背景:业务需求要用到 imagefttext 函数,发现GD库一开始安装时没有添加 FreeType linux版本 centos 6.6 安装流程(由于服务器为分布式内网服务器,无法使...

PHP内核介绍及扩展开发指南—基础知识

PHP内核介绍及扩展开发指南—基础知识

一、 基础知识   本章简要介绍一些Zend引擎的内部机制,这些知识和Extensions密切相关,同时也可以帮助我们写出更加高效的PHP代码。   1.1 PHP变量的存储   1.1...

PHP 多进程 解决难题

而且, 如果输入数据非法, 而脚本没有检测, 导致abort, 也会让你很不开心. 那? 怎么办呢? 呵呵, 别着急, 多进程来帮您! 那,这是为什么呢? 优点: 1. 使用多进程, 子...

解析php中两种缩放图片的函数,为图片添加水印

有两种改变图像大小的方法.(1):ImageCopyResized() 函数在所有GD版本中有效,但其缩放图像的算法比较粗糙.(2):ImageCopyResampled(),其像素插值...

php函数array_merge用法一例(合并同类数组)

复制代码 代码如下:$arr1 = $dblink->mem_fetch_array ( "SELECT t_pid,imgname,invented,score FROM `t_...