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

7
  • 2
    You never use $i, so you can change it to while( strlen($string) < 4) Commented Mar 13, 2012 at 0:28
  • 1
    Server not found error?? What's that got to do with php syntax? Commented Mar 13, 2012 at 0:28
  • I see absolutely nothing wrong with it.. I'll keep looking thought.. Commented Mar 13, 2012 at 0: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. Commented Mar 13, 2012 at 0: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... Commented Mar 13, 2012 at 0:30

2 Answers 2

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';
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Well, your PHP is valid and works for me - problem must be elsewhere (perhaps in some of the code you've omitted?)
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.
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';

1 Comment

Thanks Brian for testing the code, I edited the code for use as an example, not a real code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.