Home > Backend Development > PHP Tutorial > 帮忙解释两个for循环解决方法

帮忙解释两个for循环解决方法

WBOY
Release: 2016-06-13 12:06:01
Original
1405 people have browsed it

帮忙解释两个for循环
各位大大,这是一个用*输出倒直角三角形的例子,这两个for循环我不是很白,特别是里面的那个,能解释下么,谢谢啦~

    for($i=1;$i<15;$i++){<br />        for($j=$i;$j<15;$j++){<br /><br />            echo "*";<br />        }<br />       echo "<br>";<br />    }
Copy after login

------解决方案--------------------
<br /><br />$j = $i;<br />//是让内层循环的起始值等于外层的值<br />//每次都少输出一个*<br />//外层第一次循环,内层输出14个*<br />//外层第二次循环,内层输出13个*<br />//以此类推只到循环结束<br /><br /><br />
Copy after login

------解决方案--------------------
$i 可以看做是行, $j可以看做是列
首先看 $i =1;的时候$j =1; j然后看 $i =2;的时候$j=2;j…………
一次循环,最后得到第一行14个*,共14行,最后一行一个*,不知是否清楚.

Related labels:
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