user7147407 - 1 year ago 76
PHP Question

# PHP Dubble For Loop

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>";

};
};
};
``````

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download