Jump to content

While loop slowing browser down


kworld

Recommended Posts

Hey all,

 

This bit of code is currently slowing my browser load to the point of this message;

Fatal error: Maximum execution time of 60 seconds exceeded.

 

Can anybody help me please its like the loop continues even though its already calcultaed what needs to be done. :confused:

 

		while(($pricecalc != $dprice)||($pricecalc>$dprice))
			{
			$pricecalc = ($pricecalc-$dprice);
			}

 

Link to comment
https://forums.phpfreaks.com/topic/200405-while-loop-slowing-browser-down/
Share on other sites

I think you want && and not || because it can be the case that $pricecalc will never equal the value of $dprice so that first part will always remain true.

 

But with just that snippet, I can't say for certain that && is what you should use. Please explain what that loop should do.

The || are for an OR operator, It is meant to either be equal or be under the standard price if it goes below the standard price the while loop will stop.

 

The calculations seem to get done correctly and be displayed but only when it has a 60 second time out,  i think the loop doesnt want to stop for some reason.

 

Yes, but that's not what your code says.

 

It says:

Run $pricecalc = ($pricecalc-$dprice);

if one of the following is true:

  1. $pricecalc != $dprice

  2. $pricecalc>$dprice

 

So even if $pricecalc is less than $dprice (meaning #2 will be false), that doesn't mean $pricecalc is equal to $dprice, so since #1 is true, the while loop continues.

 

To do what you want, you probably want this: (if I understand the issue correctly)

if ($pricecalc != $dprice) {
     while ($pricecalc > $dprice) $pricecalc -= $dprice;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.