php PHP在for循环内嵌套for循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27212125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP nesting for loop inside for loop
提问by Dan
Been trying to figure this one out for the last 3 hours and so hard to find any search results for "nesting for loop inside for loop", so I must ask this question and somebody can help clarify, as much of an amateur question this is, will much appreciate any and all help.
在过去的 3 个小时里一直试图弄清楚这个问题,但很难找到“在 for 循环中嵌套 for 循环”的任何搜索结果,所以我必须问这个问题,有人可以帮助澄清,这是一个业余问题,将不胜感激任何和所有帮助。
So I have this block of code underneath, for a beginners looping exercise I found at http://www.w3resource.com/php-exercises/php-for-loop-exercises.phpon question 3 of that page. The end result should be on that page as well.
所以我在下面有这个代码块,对于初学者循环练习,我在http://www.w3resource.com/php-exercises/php-for-loop-exercises.php上找到了该页面的问题 3。最终结果也应该在该页面上。
<?php
for($a = 1; $a <= 5; $a++) {
for($b = 1; $b <= $a; $b++) {
echo "*";
if($b < $a) {
echo " ";
}
}
echo "<br />";
}
?>
It turns out like a triangle shape made up of 15 little *
原来是一个三角形的形状,由 15 个小 *
I am trying to figure out exactly how this works, as even though the site has an answer, I want to understand so I can write things myself instead of copy pasting.
我正试图弄清楚这是如何工作的,即使该网站有答案,我也想了解一下,这样我就可以自己写东西而不是复制粘贴。
Below is what I have been able to make out after reading php.net
and also trying to look for a solution on Google.
以下是我在阅读php.net
并尝试在 Google 上寻找解决方案后所得出的结论。
So, the $a
for
loop starts off as true so loop continues onto the $b FOR
loop, it is true so it will echo *
, At this point $b
is not < $a
as loop is not yet over so the ++
have not been added yet and if
statement is false, so we <br />
and re-run loop.
所以,$a
for
循环开始为真所以循环继续$b FOR
循环,它是真的所以它会回声*
,此时$b
不是< $a
因为循环还没有结束所以++
还没有被添加并且if
语句是假的,所以我们<br />
重新- 运行循环。
Second time around. Now $a=2
and for
loop is still true so it will go on to $b
for loop and echo *
. This is where I get confused. The $b
has ++
at the end of its statement, so doesn't it mean that after loop 1 $b = 2
and if
is still false ?
第二次来了。现在$a=2
and for
loop 仍然是真的,所以它会继续$b
for loop 和 echo *
。这是我感到困惑的地方。将$b
有++
在发言结束,所以并不意味着该循环1后$b = 2
和if
仍然是假的?
In my head I keep thinking it should print out like:
在我的脑海中,我一直认为它应该打印出来:
<br />* <br /> * <br />
<br />* <br /> * <br />
instead of this:
而不是这个:
<br />* <br /> * * <br />
<br />* <br /> * * <br />
My conclusion.
我的结论。
- The
$b
value will reset back to 1 after each loop and repeats itself untilif
statement is false.
$b
每次循环后该值将重置回 1 并重复自身直到if
语句为假。
OR
或者
- The
*
that get printed are stored and will be the automatic starting point at the beginning of each loop. I understand that if doing a simple print for afor($i=0;$i<11;$i++)
loop will carry the value over, however in my situation, a printed*
is not a value, so it shouldn't be carried over to a new loop.
- 的
*
是那些获得的印刷被存储并且将在每个循环的开始自动起点。我知道如果为for($i=0;$i<11;$i++)
循环做一个简单的打印会携带这个值,但是在我的情况下,打印*
的不是一个值,所以它不应该被转移到一个新的循环中。
OR
或者
- I am just completely off with both my conclusions.
- 我完全不同意我的两个结论。
Either way, its been confusing me so badly.
无论哪种方式,它都让我非常困惑。
Sorry for long explanation.
抱歉解释太长了。
Thanks.
谢谢。
回答by Hanky Panky
Let's take things one step at a time
让我们一步一步来
Outer Loop
外环
for($a = 0; $a <= 5; $a++) {
//inner loop here
echo "<br>"; //after the inner loop has executed, start a new line
}
Start at 0, go up to 5 and do whatever is written inside. Now that means two things will happen for each iteration
从 0 开始,到 5 并执行里面写的任何操作。现在这意味着每次迭代都会发生两件事
1) Inner loop will be executed for every iteration of the outer loop
1) 外循环的每一次迭代都会执行内循环
2) An HTML row break will be printed for every iteration of the outer loop
2) 外循环的每次迭代都会打印一个 HTML 换行符
Now what does the Inner Loopdo?
现在内循环做了什么?
for($b = 1; $b <= $a; $b++) {
echo "*";
if($b < $a) {
echo " "; // $b<$a means we still have more stars to print on this line
}
}
Start at 1, go up to the value of $a
, this $a will come from the outer loop and will be 1 for the first run, 2 for second up to 5. And while doing this, print a *
; that means for the first time 1 star will be printed because loop will start at 1 and end at 1. For the second time 2 stars, 3rd time three stars and so on.
从 1 开始,一直到 的值$a
,这个 $a 将来自外部循环,第一次运行时为 1,第二次运行时为 2,直到 5。在执行此操作时,打印 a *
; 这意味着第一次将打印 1 颗星,因为循环将从 1 开始并在 1 处结束。第二次 2 颗星,第三次 3 颗星,依此类推。
Now after printing the star, see if this is the last star that this iteration had to print, if it is not that then print a space so that the next time a star is printed there is a space in between.
现在打印星星后,看看这是否是本次迭代必须打印的最后一颗星星,如果不是,则打印一个空格,以便下次打印星星时中间有一个空格。
That's about it :) Note that your outer loop starts at 1, it should start at 0 to form the required shape correctly.
就是这样 :) 请注意,您的外循环从 1 开始,它应该从 0 开始以正确形成所需的形状。
Now if you are still confused about the $b<$a
thing, just get rid of it and run your code again and you will notice nothing much has changed except that there are no spaces between the stars and it will be a little easier to understand
现在,如果您仍然对这$b<$a
件事感到困惑,只需摆脱它并再次运行您的代码,您会发现除了星星之间没有空格之外没有任何变化,并且会更容易理解
for($b = 1; $b <= $a; $b++) {
echo "*";
}
And oh, ++
after a variable means increment its value by 1. In a for loop that means on the next run add 1 to the value of the counter. You can also write it as
哦,++
在变量之后意味着将其值增加 1。在 for 循环中,这意味着在下一次运行时将计数器的值加 1。你也可以把它写成
for($a=0; $a<=5; $a=$a+1) // thats the same as $a++;
If you wanted the value to increment by more than 1 you could say
如果您希望该值增加超过 1,您可以说
for($a=0; $a<=5; $a=$a+2) // a will be 0 , 2, 4