2

I have this loop

while (count($arr) < 7)
        {

            $string = 'xx';
            for ($i=0; strlen($string) < 4; $i++)
            {
                $string = $string.'1';
            }
            echo "<br>array length is less than 7, word $string is created.";
            $arr[] = $string;
        }

Every time I run this part of the code, my xampp local server would time out and gives the server not found error.

I have found that if I delete the inner for loop, it would run fine. Is there anything wrong with putting the for loop inside the while loop?

Also, my conditional statement strlen($string) < 4 in the for loop does not have any reference to the $i variable, but I don't see any thing not logical of having a conditional statement not related to the counter. Am I wrong, does it require some sort of comparison against the counter?

TIA

Jamex
  • 1,164
  • 5
  • 22
  • 34
  • 2
    You never use `$i`, so you can change it to `while( strlen($string) < 4)` – nickb Mar 13 '12 at 00:28
  • 1
    Server not found error?? What's that got to do with php syntax? – Adam Mar 13 '12 at 00:28
  • I see absolutely nothing wrong with it.. I'll keep looking thought.. – DanRedux Mar 13 '12 at 00:28
  • at first glance it looks fine & nothing to do with nested loops. Personally I use NetBeans; you might choose Eclipse, Zend, etc. But, if you don't have a debugger to step through your code you are not going to get far. – Mawg says reinstate Monica Mar 13 '12 at 00:29
  • 1
    Nick, she may want to use it later. Nothing wrong with it, unless you suspect it's causing the error somehow.. The only way it could be causing an error is if $string was assigned before to it by reference, which I doubt. $string=&$i would cause errors like this... – DanRedux Mar 13 '12 at 00:30
  • The exact code works perfectly for me. Don't see anything wrong with it, either. – data Mar 13 '12 at 00:30
  • Thanks all, I don't see anything wrong with the code either, but the for loop is somehow cause the server to hang. Also, I do use the $i variable for something else. – Jamex Mar 13 '12 at 02:49

2 Answers2

1

Nothing wrong with having a for inside a while.

Your "for" would be better a bit clearer as

while(strlen($string) < 4) {
    $string = $string.'1';
}
John3136
  • 28,809
  • 4
  • 51
  • 69
  • Thanks John, the reason I have $i is because I am using $i as an array index, that part of the code was left out. – Jamex Mar 13 '12 at 02:45
  • Well, your PHP is valid and works for me - problem must be elsewhere (perhaps in some of the code you've omitted?) – John3136 Mar 13 '12 at 02:53
  • 1
    Thanks again, I am walking through my very short omitted code to see if there is any thing else. At least I know the error is not due to the loops. – Jamex Mar 13 '12 at 03:34
0

I don't see any issues whatsoever with your php.

I copied your code exactly and there was no infinite loop at all.

The result I got was as follows:

<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.
<br>array length is less than 7, word xx11 is created.

My only suggestion would be to change:

$string = $string.'1';

to

$string .= '1';
iambriansreed
  • 21,935
  • 6
  • 63
  • 79
  • 1
    Thanks Brian for testing the code, I edited the code for use as an example, not a real code. – Jamex Mar 13 '12 at 02:47