Home > Backend Development > PHP Tutorial > A brief discussion on the sixth bullet of PHP - using for loop to output the multiplication table_PHP tutorial

A brief discussion on the sixth bullet of PHP - using for loop to output the multiplication table_PHP tutorial

WBOY
Release: 2016-07-13 17:50:31
Original
1288 people have browsed it

Last time http://www.BkJia.com/kf/201204/128751.html I told you about the Nine Yang Magic of paging. I don’t know how well you master it. I hope it can help you!

These days I found that some children's shoes still have some problems in the loop of outputting the multiplication table. Today I will explain it to you in detail...

First let’s look at the code:
for($i=1;$i<=9;$i++){
for($j=1;$j<=$i;$j++){
echo "$j x $i = ".$j*$i." ";
}
echo "
";
}
?>
Output result:

Okay, let’s analyze it step by step:

We call the outermost loop a "trip":

First trip:

$i is 1, satisfying $i <= 9,

And the inner loop $j satisfies $j <= $i, output 1 x 1 = 1

$j increases by 1, no longer satisfies $j <=$i, and no longer outputs

Output "
" line break,

$i increments itself by 2

This trip is over.

Second trip:

$i is 2, satisfies $i < = 9, and starts executing the second loop

At this time, the condition of the inner loop becomes:

for($j =1 ; $j < = 2 ; $j++){
........
}

The inner loop outputs twice, respectively:

1 x 2 = 2 and 2 x 2 = 4

Similarly, when $j in the inner loop increases to 3, the inner loop will no longer be executed

Output "
";

$i increments to 3

This trip is over!

Third trip:

Same as above....

And so on:

Until the ninth trip:

At this time $i has been increased to 9

$i <= 9 still holds

Execute the ninth inner loop:

At this time, the inner loop becomes

for($j=1 ; $j <=9 ; $j++){
.......
}

Loop and execute the code in the loop body 9 times,

are respectively

1 x 9 = 1 2 x 9 =18..........9 x 9 = 81

That is the last line of the result

At this time $j no longer satisfies $j<=9, and the inner loop execution ends

Output "
"

$i increases to 10

This trip is over.

By the tenth time, $i is already 10 and no longer satisfies $i<=9. At this time, this cycle will no longer be executed.
Finally, the multiplication table shown in the picture above is output.

How about it? I don’t know if you understand it. If you understand it, you can try to output the multiplication table in reverse! The principle is the same!



Author zdrjlamp

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478267.htmlTechArticleI told you about it last time http://www.2cto.com/kf/201204/128751.html I don’t know how well you have mastered the Nine Sun Magic Skills of pagination. I hope it can help you! These days I find that some children...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template