This forum is in permanent archive mode. Our new active community can be found here.

Need PHP help

edited March 2006 in Technology
$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

  • edited March 2006
    Well, first of all why are you asking this here? There are plenty of PHP forums out there.

    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.
    Post edited by Apreche on
  • edited March 2006
    I think I'm going to make a "Scott teaches programming" episode or series. Perhaps after we do a series about how computers work.
    Post edited by Apreche on
  • edited March 2006
    "Secondly, the line where you have

    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.
    Post edited by RadioElectric on
  • edited March 2006
    Yeah, if anyone needs programming help I know A LOT of languages. So many I can't remember them all. I also know at least two other people in this forum who are as good as, if not better than, I am when it comes to programming. They might help also.

    But let's not get carried away. This is the GeekNights forum, we don't want it to become the programming help forum.
    Post edited by Apreche on
  • If you created a "Tech Help" category it wouldn't matter how much it was discussed.
  • Yeah, but I don't want to encourage people to start using this as a tech help forum. Making a category would be like an open invitation. It's bad enough dealing with people I know calling me and stupid people at work.
Sign In or Register to comment.