1

Is the only difference between sleep function and usleep one Is that the parameter of first one is in seconds and the other one in micro seconds ?? Is there any difference else ??

Anther thing please, I'll use this functions with loops, Is there any problem will accident me on that??

Nedal Eldeen
  • 159
  • 1
  • 7

3 Answers3

6

From a review of the PHP documentation on usleep and that of sleep, you'll notice that there are 2 differences:

  1. The argument for usleep is an integer representing micro seconds (a micro second is one millionth of a second), while the argument for sleep is an integer representing seconds.

  2. sleep returns "zero on success, or FALSE on error." usleep has no return value. Here are more details on the return value for sleep:

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.

In general, you should be ok using these functions within loops. That being said, though, the more important questions to ask are: Why do you need a solution that depends on halting execution for a certain amount of time? Is this really a good solution to your problem, or is it a hack-fix for some strange bug or edge case that you just want to have go away?

Alvin S. Lee
  • 4,984
  • 30
  • 34
-1

usleep — Delay execution in microseconds. Halt time in micro seconds. A micro second is one millionth of a second.

`sleep` — Delay execution

Delays the program execution for the given number of seconds.

i don't get more difference between this two function

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
-2

sleep() allows your code to sleep in seconds.

sleep(5); // sleeps for 5 seconds

usleep() allows your code with respect to microseconds.

usleep(2500000); // slees for 2.5 seconds

Other than that, I think they're identical.

sleep($n) == usleep($n * 1000000)
usleep(25000) only sleeps for 0.025 seconds.
Shri Suresh
  • 463
  • 1
  • 5
  • 20