Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do these nested while loops not work?

I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?

<?php
$x = $y = 10;

while ($x < 100) {
    while ($y < 100) {
        $num = $x * $y;
        $numstr = strval($num);
        if ($numstr == strrev($numstr)) {
            $pals[] = $numstr;
        }
        $y++;
    }
    $x++;   
}
?>
like image 592
aliov Avatar asked Sep 16 '26 00:09

aliov


1 Answers

you should reset y=10 inside the first while.

$x = 10;

while ($x < 100) {
    $y = 10;
    while ($y < 100) {
        $num = $x * $y;
        $numstr = strval($num);
        if ($numstr == strrev($numstr)) {
            $pals[] = $numstr;
        }
        $y++;
    }
    $x++;   
}
like image 177
Marco Mariani Avatar answered Sep 18 '26 13:09

Marco Mariani