Script Stops

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aubrey
Forum Newbie
Posts: 2
Joined: Thu Apr 01, 2010 5:03 am

Script Stops

Post 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?
jbulaswad
Forum Newbie
Posts: 14
Joined: Tue Mar 30, 2010 2:37 pm
Location: Detroit, Michigan, USA

Re: Script Stops

Post 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.
Aubrey
Forum Newbie
Posts: 2
Joined: Thu Apr 01, 2010 5:03 am

Re: Script Stops

Post 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.
Post Reply