一、背景

打算写一个专题,记录PHP踩过的坑,以sleep函数开篇。

二、函数说明

int sleep ( int $seconds )

程序延迟执行指定的 seconds 的秒数。
成功时返回 0,错误时返回 FALSE。

三、坑点

参数传入小数,例如程序中sleep(0.5)不能起到作用,因为0.5转化成整型为0。即sleep(0),所以不能起到延缓执行的作用。

四、解决方案

使用usleep函数

void usleep ( int $micro_seconds )

以指定的微秒数延缓程序的执行。例如延缓0.5秒执行,即usleep(500000)。
附官网手册下的一则评论:

This may seem obvious, but I thought I would save someone from something that just confused me: you cannot use sleep() to sleep for fractions of a second. This:

<?php sleep(0.25) ?>

will not work as expected. The 0.25 is cast to an integer, so this is equivalent to sleep(0). To sleep for a quarter of a second, use:

<?php usleep(250000) ?>

相关文章:

  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2021-07-23
  • 2022-02-01
  • 2021-09-22
  • 2021-05-21
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2021-07-20
  • 2022-12-23
相关资源
相似解决方案