Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.11     Pausing script execution: sleep() and usleep()

This is NOT the latest copy of this book; click here for the latest version.

void sleep ( int seconds)

void usleep ( int micro_seconds)

When you want to pause execution in your script, there are two ways you can implement the code. Some people (thankfully very few) choose the first option and write code like this:

<?php
    $now
= time();
    while (
$now + 4 > time()) {
        
// do nothing
    
}

    echo
"Done.\n";
?>

While it does work, there are two problems with it. Firstly, time() has a very low precision, only returning the number of whole seconds that have passed, which makes the whole thing quite vague. Secondly, PHP has to sit there looping thousands of times while it waits, essentially doing nothing. A much better solution is to use the one of the two script sleep functions, sleep() and usleep(), which take the amount of time to pause execution as their only parameter.

The difference between sleep() and usleep() is that sleep() takes a number of seconds as its parameter, whereas usleep() takes a number of microseconds - millionths of a second - as its parameter. Using either of them is far more accurate than the previous time() loop, and they both have their advantages - sleep() is better if you do not need the accuracy, and usleep() is better if you do. Simple, really!

The above script could be rewritten like this:

<?php
    sleep
(4);
    echo
"Done\n";
?>

Or this:

<?php
    usleep
(4000000);
    echo
"Done\n";
?>

Note that the default maximum script execution time is 30 seconds, but you can use sleep() and usleep() to make your scripts go on for longer than that because technically PHP does not have control during the sleep operation.

Author's Note: PHP 5 rocks, I know, but not everyone uses it yet, and neither will they for some considerable time. The use of usleep() is a big no-no if you want backwards compatibility, because it wasn't available on Windows prior to PHP 5.





<< 4.10 Extension functions: get_loaded_extensions(), get_extension_funcs(), dl(), and extension_loaded()   4.12 Executing external programs: exec(), passthru(), and virtual() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
one example - 06 Sep 2008

I have wrote scripts that serves file for downloading via PHP script. And i want to limit upload speed. The only way is to execute this script using sleep function to reduce speed (for example for non-registered users)

A PHP User - 06 Sep 2008

//// if i have a code like
//// I want that a message will print on screen after that
////it goes to a page. But it is not working.
<?
echo "This message will print";
sleep(5);
header("Location:samepage.php")
?>

cast3r - 06 Sep 2008

i think readers should first look at php manual before asking such questions. ie. for sleep() there are many comments about
defending against DoS attacks.

A PHP User - 06 Sep 2008

maybe you can use it whit a loop when yuo are generating a mass mail system so, if yuo got 300 mails, put a sleep() into a loop and that way you dont overflow the server sendmail system.

Guna - 06 Sep 2008

PHP used in wep application as well as for OS - Operating System level operation. Like shell script OR batch file Or as an EXE file running in OS. There pausing may be needed at some time.

A PHP User - 06 Sep 2008

Maybe for non-web uses, also, maybe a php game or a ghetto redirect.

Ganesan Rajagopal - 06 Sep 2008

I am not clear why anyone would want to sleep while generating a web page.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow