PHP: Hypertext Preprocessor (original) (raw)
sleep
(PHP 4, PHP 5, PHP 7, PHP 8)
sleep — Delay execution
Description
Delays the program execution for the given number ofseconds
.
Note:
In order to delay program execution for a fraction of a second, use usleep() as the sleep() function expects an int. For example,
sleep(0.25)
will pause program execution for0
seconds.
Parameters
seconds
Halt time in seconds (must be greater than or equal to 0
).
Return Values
Returns zero on success.
If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be192
(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.
Errors/Exceptions
If the specified number of seconds
is negative, this function will throw a ValueError.
Changelog
Version | Description |
---|---|
8.0.0 | The function throws a ValueError on negative seconds; previously, an E_WARNING was raised instead, and the function returned false. |
Examples
Example #1 sleep() example
<?php// current time echo date('h:i:s') . "\n";// sleep for 10 seconds sleep(10);// wake up ! echo date('h:i:s') . "\n";?>
This example will output (after 10 seconds)
See Also
- usleep() - Delay execution in microseconds
- time_nanosleep() - Delay for a number of seconds and nanoseconds
- time_sleep_until() - Make the script sleep until the specified time
- set_time_limit() - Limits the maximum execution time
Found A Problem?
11 years ago
`re: "mitigating the chances of a full bruit force attack by a limit of 30 lookups a minute."
Not really - the attacker could do 100 requests. Each request might take 2 seconds but it doesn't stop the number of requests done. You need to stop processing more than one request every 2 seconds rather than delay it by 2 seconds on each execution.
`
21 years ago
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.
barlow at fhtsolutions dot com ¶
13 years ago
You should put sleep into both the pass and fail branches, since an attacker can check whether the response is slow and use that as an indicator - cutting down the delay time. But a delay in both branches eliminates this possibility.
9 years ago
`Maybe obvious, but this my function to delay script execution using decimals for seconds (to mimic sleep(1.5) for example):
`
1 year ago
`To avoid using usleep function that is not working on every operating system, below a function that should have the same behavior :
function wait(int $millisecond = 0) {
if (0 !== $millisecond) {
seconds=(int)(seconds = (int) (seconds=(int)(millisecond / 1000);
nanoSeconds=(nanoSeconds = (nanoSeconds=(millisecond % 1000) * 1000000;
time_nanosleep($seconds, $nanoSeconds);
}
}
`
6 years ago
Diego Andrade's msleep function is not compatible with php7's `strict_types`, cast the usleep parameter to int, and it will be, usleep((int)($time * 1000000));
24 years ago
`it is a bad idea to use sleep() for delayed output effects as
you have to flush() output before you sleep
depending on your setup flush() will not work all the way to the browser as the web server might apply buffering of its own or the browser might not render output it thinks not to be complete
netscape for example will only display complete lines and will not show table parts until the tag arrived
so use sleep if you have to wait for events and don't want to burn to much cycles, but don't use it for silly delayed output effects!
`
11 years ago
`If you are having issues with sleep() and usleep() not responding as you feel they should, take a look at session_write_close()
as noted by anonymous on comments;
"If the ajax function doesn't do session_write_close(), then your outer page will appear to hang, and opening other pages in new tabs will also stall."
`
3 years ago
`I wrote a simple method for sleeping with a float, which also allows you to do milliseconds (via fractional seconds).
intSecs=intval(intSecs = intval(intSecs=intval(secs); microSecs=(microSecs = (microSecs=(secs - $intSecs) * 1000000; if( $intSecs > 0) { sleep($intSecs); } if($microSecs > 0) { usleep($microSecs); } } ?>And testing on my machine it works perfectly:
xasx as xassecs) { $t = microtime(true); sleepFloatSecs($secs); t=microtime(true)−t = microtime(true) - t=microtime(true)−t; echo "$secs \t => \t $t\n"; } ?>Output:
0.10017800331116 0.25 => 0.25016593933105 0.5 => 0.50015211105347 1 => 1.0001430511475 1.5 => 1.5003218650818 2 => 2.000167131424 2.5 => 2.5002470016479 ?>`
2 years ago
From my testing calling sleep(0); will do a `thread spin`. It would be nice if this was to be put explicitly in the documentation as it is useful. You can do the minimum wait without overloading the CPU thread.
4 years ago
`An example of using sleep to run a set of functions at different intervals. This is not a replacement for multi-threading, but it could help someone that wants to do something cheap. You don't have to use eval(). It is just used as an example. This is different than running a standard 1 second sleep loop, due to sleeping longer does not consume as much CPU.
mintime=key(mintime = key(mintime=key(sleeptimers); foreach($sleeptimers as SleepTime=>SleepTime => SleepTime=>Jobs) { foreach($Jobs as JobIndex=>JobIndex => JobIndex=>JobDetail) { if(!isset($JobDetail['lastrun'])) { sleeptimers[sleeptimers[sleeptimers[SleepTime][$JobIndex]['lastrun'] = time(); if($SleepTime < mintime)mintime) mintime)mintime = $SleepTime; } elseif(($currenttime - JobDetail[′lastrun′])>=JobDetail['lastrun']) >= JobDetail[′lastrun′])>=SleepTime) { eval($JobDetail['func']); lastrun=time();<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>s</mi><mostretchy="false">[</mo></mrow><annotationencoding="application/x−tex">sleeptimers[</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal">ee</span><spanclass="mordmathnormal">pt</span><spanclass="mordmathnormal">im</span><spanclass="mordmathnormal">ers</span><spanclass="mopen">[</span></span></span></span>SleepTime][lastrun = time(); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>s</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">sleeptimers[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ee</span><span class="mord mathnormal">pt</span><span class="mord mathnormal">im</span><span class="mord mathnormal">ers</span><span class="mopen">[</span></span></span></span>SleepTime][lastrun=time();<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mi>r</mi><mi>s</mi><mostretchy="false">[</mo></mrow><annotationencoding="application/x−tex">sleeptimers[</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal">ee</span><spanclass="mordmathnormal">pt</span><spanclass="mordmathnormal">im</span><spanclass="mordmathnormal">ers</span><spanclass="mopen">[</span></span></span></span>SleepTime][JobIndex]['lastrun'] = lastrun;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>y</mi><mi>s</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">mysleeptime=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">ys</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal">ee</span><spanclass="mordmathnormal">pt</span><spanclass="mordmathnormal">im</span><spanclass="mordmathnormal">e</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>SleepTime−(lastrun; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>y</mi><mi>s</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">mysleeptime = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">ys</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ee</span><span class="mord mathnormal">pt</span><span class="mord mathnormal">im</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>SleepTime - (lastrun;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>m</mi><mi>y</mi><mi>s</mi><mi>l</mi><mi>e</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>i</mi><mi>m</mi><mi>e</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">mysleeptime=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">m</span><spanclass="mordmathnormal">ys</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal">ee</span><spanclass="mordmathnormal">pt</span><spanclass="mordmathnormal">im</span><spanclass="mordmathnormal">e</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>SleepTime−(currenttime - $lastrun); if($mysleeptime < 0) $mysleeptime = 0; if(($currenttime - JobDetail[′lastrun′])<JobDetail['lastrun']) < JobDetail[′lastrun′])<mintime) mintime=mintime = mintime=mysleeptime; // account for length of time function runs echo 'Sleep time for function ' . JobDetail[′func′].′=′.JobDetail['func'] . ' = ' . JobDetail[′func′].′=′.mysleeptime . PHP_EOL; } } } echo 'Sleeping for ' . $mintime . ' seconds' . PHP_EOL; sleep($mintime); }?>`