1

I need to make a loop in php that says 1*9 = 9 2*9 = 18 and 1*8 = 8 and 1*7 = 7 etc in loop so the result will be this:

1*7 = 7
2*7 = 14
3*7 = 21
1*8 = 8
2*8 = 16
3*8 = 24
1*9 = 9
2*9 = 18
3*9 = 27

And I need to use a nested loop for that i tried some things out but i cant get it to work here is what is did. But for the most part i don't really understand what nested loops do and what there used for. hope you can help me thanks!

    for ($tafel7 = 0; $tafel7 <= 9; $tafel7++) {
        for($tafel7 = 0; $tafel7 <= 9; $tafel7++){
            for($tafel7 = 0; $tafel7 <= 9; $tafel7++){

            $antwoord9 = $tafel7 * 9;
            echo "$tafel7 x 8 = $antwoord9 <br>";

            $antwoord8 = $tafel7 * 8;
            echo "$tafel7 x 8 = $antwoord8 <br>";

            $antwoord7 = $tafel7 * 7;
            echo "$tafel7 x 7 = $antwoord7 <br>";


            };
        };
    };
4
  • try different variable names in loops Commented Nov 21, 2016 at 7:14
  • Clarify what are you trying to achieve. You can just provide input parameters. Commented Nov 21, 2016 at 7:23
  • eval.in/681631 . it will be easy to understand Commented Nov 21, 2016 at 7:25
  • bly jy het reg gekom, al was ek die eerste ou wat gehelp het. Commented Nov 21, 2016 at 8:22

3 Answers 3

1

Your question is not as clear but if you just want the exact result, here's the code:

for($i = 7; $i <= 9; $i++){
    for($j = 1; $j <= 3; $j++){
        $result = $i * $j;
        echo $j." * ".$i." = ".$result."<br>";
    }
}

If you need to print complete tables, then try this:

for($i = 1; $i <= 9; $i++){
    for($j = 1; $j <= 10; $j++){
        $result = $i * $j;
        echo $i." * ".$j." = ".$result."<br>";
    }
}

As far as the explanation is concerned, then listen:

A normal loop works by executing a block of statement(s) again and again until a condition is met. So, nested loops are basically loops in another loop. This means that the block of code will be executed 'n' number of times by the inner loop, and then the inner loop will again be executed by the outer loop, again 'n' number of times. 'n' means whatever the number of times the conditions are met for the respective for loops. Hope you got my explanation!

Sign up to request clarification or add additional context in comments.

Comments

0

You need to loop over your two variables, (1) the left operand and (2) the right operand

for ($right = 7; $right <= 9; $right++) {
    for ($left = 1; $left <= 3; $left++) {
        $product = $left * $right;
        echo "{$left} x {$right} = {$product} <br>";
    }
}

Comments

0

In this example, I have to loops one with the times table you want eg 7, 8 and 9 "tafel" and the other one with how many times you want it to run for each times table "$hoeveel". This can easily be altered to more times tables or how many times you want to run, you also only need to export the values once as it cycles through the loops anyways. Hope it helps.

for ($tafel = 7; $tafel <= 9; $tafel++) {
    for ($hoeveel = 1; $hoeveel <= 3; $hoeveel++) {
        $antwoord = $tafel * $hoeveel;
        echo "$tafel x $hoeveel = $antwoord <br>";
    }
}

3 Comments

I made it use the 7,8 and 9 times table and only does the first 3 of each you can always change the values, you were using the same variables and to many nested loops.
I'd upvote this answer if you edit the answer explaining what happens with this code, since OP has no clue what he's doing with his nested for loops at the moment.
sorry about that changed it to give a bit more explanation

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.