You need to reset $x
, so:
$i = 1;
$x = 1;
while($i <= 5){
while($x <= 5){
echo $i * $x;
$x++;
}
$x = 1; // added this line
echo "<br>";
$i++;
}
Output:
12345
246810
3691215
48121620
510152025
You can then do what ever you want to format it.
More elabrate explanation:
It enters both outer and inner loops, showing the desired output for the first line. You end up with $i = 2
and $x = 6
.
Since $i
is 2
, it doesn't leave the outer loop, but $x
is 6
, so it doesn't enter the inner loop again.
It then keeps adding 1
to $i
until it doesn't match the outer loop condition anymore and leaves you with that unwanted result.