$foundmyman = 0;
$itcount = 0;
//while ($foundmyman = 0)
while ($itcount < 10)
{
echo $itcount;
$itcount++;
if ($itcount > 5)
{
echo 'infinite loop error';
exit();
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm trying to get a while loop working that has an escape thing in case my php goes wrong and I make an infinite loop, or if too many iterations are done.
It wasn't working so I put this in a new php file. It will only do anything (print "12345infinitelooperror") if the uncommented condition is used.
It seems like it might be something built into php to prevent infinite loops (by not allowing loops that don't reference the controlling variable) but that seems silly.
Any ideas as to why it's not working?
Comments
Second of all, I happen to be a PHP professional. It was a good idea to post this here where you can get free help from me.
First things first. I need to know what you are really trying to do on a higher level. There is probably a better way to do it than what you are attempting. I could help you more if you gave me a bigger picture. I definitely feel a problem on the design level.
Secondly, the line where you have
while( $foundmyman = 0 )
you probably meant
while( $foundmyman == 0 )
A classic beginner's programming mistake. '=' is an assignment operator. '==' is a boolean operator. Big difference.
Third, if you want to limit something to a certain number of iterations, why are you writing an infinite loop? Use a "for" loop instead of a "while" loop when you know how many iterations you want.
And lastly, I'm getting this feeling that what you want to do is unecessary. You say you want to have an "escape thing" if your php goes wrong? What you do is you keep debugging your program until nothing goes wrong. Also, you need to learn a few things about exception and error handling. There are ways to write code that executes when "things go wrong" other than exit() or die(). Learn those ways to increase your programming powers.
while( $foundmyman = 0 )
you probably meant
while( $foundmyman == 0 )"
Yeah, someone on another forum pointed that out to me, in 200pt text, I already knew it but I just didn't apply my knowledge.
I was trying to make debugging as painless as possible, wouldn't an infinite loop hang the webserver? The only previous programming experience I have (apart from VB which I've not often used outside of an interperator environment) is a tiny computer that I could program in BASIC on, so I didn't want to find out the hard way.
Anyway, in the end I did away with the loop completely and I'm now doing it a much better way in a third as many lines.
It's probably not a good idea to start learning PHP 2 weeks before your main coursework project is due in written in it, otherwise I would have used the for loop.
"Second of all, I happen to be a PHP professional. It was a good idea to post this here where you can get free help from me. " /That's/ why I posted it here.
But let's not get carried away. This is the GeekNights forum, we don't want it to become the programming help forum.