ConorReidd ConorReidd - 2 months ago 7
PHP Question

Why is my PHP while() inside while() not going up?

I'm using this simple while loops inside of a while loop and whats happening is the

$x
is working how i planned but the
$i
isnt.

$i = 0;
$x = 0;

while ($i <= 50) {
while ($x <= 50) {
echo $i . $x . "<BR/>";
$x++;
}
$i++;
}


I'm getting the response;

00
01
0...
050


but then the script is stopping?
$i
isn't incrementing and running the while loop 50 more times so i would have

00
01
0...
050
10
11
1...
150

Answer

The problem is that you have to initialize $x to 0 at the beginning of the outer while loop. Your $x variable will never enter the inner while loop because $x is already greater than 50

$i = 0;

while ($i <= 50) {
    $x = 0;
    while ($x <= 50) {
        echo $i . $x . "<BR/>";
        $x++;
    }
    $i++;
}
Comments