2

Im trying to creating a for loop and another for loop inside it and then display the results in 3 columns. It should look like this:

0(0) 0(1) 0(2)
1(0) 1(1) 1(2)
2(0) 2(1) 2(2)
3(0) 3(1) 3(2)

Right now I have this PHP script:

for ($x = 0; $x < 4; $x++) {
    echo "$x";
    for($y = 0; $y < 3; $y++){
        echo "($y)<br/>";
    }
}  

Thanks in advance :)

1
  • <br/> should be printed by the outer loop, not the inner loop. Commented Nov 8, 2016 at 21:40

1 Answer 1

1

The outer loop is for rows, the inner loop is for columns. So you should print <br/> in the outer loop. And since you want $x printed in each column, you need to print that in the inner loop.

for ($x = 0; $x < 4; $x++) {
    for($y = 0; $y < 3; $y++){
        echo "$x($y) ";
    }
    echo "<br/>";
}  
Sign up to request clarification or add additional context in comments.

Comments

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.