PHP nested while loop -
i tried while
inside while
print multiplication table like,
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
but got 1, 2, 3, 4, 5.
code:
$i = 1; $x = 1; while($i <= 5){ while($x <= 5){ echo $i * $x; $x++; } echo "<br>"; $i++; }
this happening because you're not resetting $x
when inner loop completes iteration. try instead:
$i = 1; while($i <= 5) { $x = 1; while($x <= 5) { echo $i * $x; $x++; } echo "<br>"; $i++; }
Comments
Post a Comment