Page 1 of 1

Script Stops

Posted: Thu Apr 01, 2010 5:11 am
by Aubrey
This script runs as expected on my local machine:

Code: Select all

<?php
$starttime = time();
$firsttime = $starttime;
$count = 0;

while($count < 7)
{
	if (time()-$starttime > 5){
		$starttime = time();
		echo $starttime-$firsttime."<br />";
		$count++;
	}
}

?>
It outputs the elapsed time in seconds every 6 seconds like this:

[text]6
12
18
24
30
36
42[/text]

But when I run it on my shared host the output looks like this:

[text]6[/text]

It just prints the number 6. Anyone have an idea why this happens?

Re: Script Stops

Posted: Thu Apr 01, 2010 8:16 am
by jbulaswad
Aubrey,

Is your intention to execute a loop every X seconds? If so you may want to try the sleep() function instead. The script below should execute just as yours did:

Code: Select all

<?php
$intSeconds = 6;
$intSecondsPassed = 0;
while ($intSecondsPassed < 60) {
	$intSecondsPassed += $intSeconds;
	echo $intSecondsPassed . "<br/>";
	sleep($intSeconds);
}
?>
Not sure what your conditions are so I set it to execute for 60 seconds.

Re: Script Stops

Posted: Thu Apr 01, 2010 4:40 pm
by Aubrey
Yes, that works on both my local machine and my shared host. Interesting.

My original code didn't generate any errors in the logs, so I guess it must be something with the host's Apache configuration rather than the PHP configuration?

Thanks.