In python, I would simply have a loop with:
time.sleep(25)
do_something()
How do I do this in PHP?
| SitePoint Sponsor |
In python, I would simply have a loop with:
time.sleep(25)
do_something()
How do I do this in PHP?
while(1 == 1)
{
sleep(20);
do_something();
}


In PHP the "time" is "understood" so you can use sleep() where the argument is in seconds. http://us3.php.net/sleep Or use usleep() with microseconds.
Great! I think I'll use while(True), if that will work.
If you plan to run this through a webserver, you may have timeout issues.

@pythonista
Off Topic:
Seeing as you seem to be a Python user, anything to contribute on this thread from todays discussions?
We'd be obliged for any input/views.
Thanks
Yes, most servers have a time limit on max execution time for running php. You should better try to use cron. And if your hosting doesn't support that then, don't worry! I have heard of external sites that offer cron services.
I checked, and my server actually doesn't set any time limit.
Anyway, using sleep() does not work. When I used sleep() the webpage didn't load for, well, I stopped after waiting 10 seconds.
I'm planning on doing this:
if (time() % 25 == 0)
{ etc.
Is this an inefficient or ugly way of doing things? (Just a reminder, the point is to get the variable $font to change every 25 seconds.)
Also, is there any way to use PHP so that one does not have to refresh the page to see things change?
Last edited by Pythonista; May 7, 2009 at 09:05.
Actually, the above is not working, even with refreshing the page.
I would think it odd if your server didn't set a limit.
Is it still in an infinite loop? If so, I believe that might be your problem.
It is not an infinite loop that way. I am sure of that.
The if statement is run everytime someone loads the page. Maybe they do set a limit. I have hostgator, and the person in the online chat said that there was no limit to the amount of time. Perhaps he made a mistake, or misunderstood the question. But, anyway, I think that it is irrelevant, because I don't plan on having a infinite loop since I've discovered that this means that the page will load the HTML only up until the loop, and then wait until the loop outputs HTML.
Oh, ok. I assumed you had left it in the while loop.
I'm not sure why the above won't work, then :/
Edit: I'm going to second the suggestion to look into cron. It's built for stuff like this.


Are you wanting to change the page after it has been rendered?
sleep() runs on the server not the client. I've used it to put delays into mail() and lengthy filesystem tasks. But if you want to change font on the client, javascript would be a better choice.Originally Posted by Pythonista
I've decided to abandon this cool but complicated way to try out different fonts on my webpage. I was also planning to do other things, like writing a program to go through different combinations, giving me 20 seconds to look at each one, and automatically refreshing the page.
I've decided to just try out fonts "manually," by changing the CSS.
Bookmarks