search
Homephp教程php手册浅谈PHP第六弹----使用for循环输出九九乘法表

上次http://www.BkJia.com/kf/201204/128751.html我给大家讲了分页的九阳神功,不知道大家掌握的如何,希望能够帮到各位童鞋!

这些天我发现有些童鞋在输出九九乘法表的循环上还是有些问题,今天我给大家详细讲解下...

首先我们来看代码:
for($i=1;$i for($j=1;$j echo "$j x $i = ".$j*$i." ";
}
echo "
";
}
?>
输出结果:

 

好,我们来一步一步分析:

我们把最外层循环一次称之为“趟”:

第一趟:

$i为1,满足$i

而内层循环$j 满足 $j

$j 自加1 ,不再满足 $j

输出 "
"换行,

$i 自加 2

本趟结束。

第二趟:

$i 为 2,满足$i

此时内层循环的条件变成了:

for($j =1 ; $j ........
}

内层循环输出两次,分别输出:

1 x 2 = 2 和 2 x 2 = 4

同理,当内层循环中的 $j 自加到 3 的时候,不再执行内层循环

输出“
”;

$i 自加为 3

本趟结束!

第三趟:

同上.....

依次类推:

直到第九趟:

此时$i 已经自加为 9

$i

执行第九趟的内层循环:

此时内层循环变为

for($j=1 ; $j ...........
}

循环执行循环体内的代码9次,

分别为

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

也就是结果的最后一行

此时$j不再满足$j

输出“

$i 自加为10

本趟结束。

至第十趟的时候,$i已经为10,不再满足$i 最终输出了我们上图显示的九九乘法表。

怎么样,不知道大家是否看明白了呢,如果明白的话,可以尝试着把此九九乘法表反过来输出!原理是相同的!



作者 zdrjlamp

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools