PHP“继续”停止 foreach 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21204747/
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 "continue" stops foreach loop
提问by RonnyKnoxville
This seems like a very stupid question, but without making any changes to the server.. the continue function in PHP seems to have started working incorrectly.
这似乎是一个非常愚蠢的问题,但没有对服务器进行任何更改.. PHP 中的 continue 函数似乎开始不正确地工作。
For example:
例如:
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
continue;
echo $test."<br>";
}
}
Outputs:
输出:
Got here
Got here
Got here
Got here
Whereas:
然而:
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
echo $test."<br>";
}
}
Ouputs:
输出:
Got here
1
Got here
3
Got here
4
Got here
5
I have used this function before and it did not seem to have this effect. Any ideas? Like I said, nothing on the server has changed so the PHP version is the same.
之前用过这个功能,好像没有这个效果。有任何想法吗?就像我说的,服务器上没有任何变化,所以 PHP 版本是相同的。
回答by BaBL86
I don't know what effect you want, but this example is working correctly. continueneeds to break current iteration and go to the next without executing code below this operator. And this function works in this case all the time since PHP 4.
我不知道你想要什么效果,但这个例子工作正常。continue需要中断当前迭代并转到下一个而不执行此运算符下面的代码。从 PHP 4 开始,这个函数一直在这种情况下工作。
回答by Mubo
I think you need to understand how continue works. i wanted to add some, so that if some body else confronts the same, may have this as reference.
我认为您需要了解 continue 是如何工作的。我想添加一些,以便如果其他人遇到相同的情况,可以将其作为参考。
You need to use the key word continueWhen you want ignore the next iteration of a loop. continue is always used with if condition
当您想忽略循环的下一次迭代时,您需要使用关键字 continue。continue is always used with if condition
According to example here .
根据这里的例子。
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
**continue;**
echo $test."<br>";
}
}
This works as expected and perfect and here is why. You are looping through the array $testers which has four elements inside and after getting every element , you are telling php to ignore the elements by using continueand that is the reason why it will not output the elements of the array $testers.
这按预期工作且完美,这就是原因。您正在循环遍历数组 $testers,其中包含四个元素,在获取每个元素之后,您告诉 php 使用continue忽略这些元素 ,这就是它不会输出数组 $testers 元素的原因。
Let me try to rewrite your Example here.
让我尝试在此处重写您的示例。
function contTest(){
$testers = array(1, 3, 4, 5);
foreach($testers as $test){
echo "Got here<br>";
if ($test == 1):
continue;
endif;
echo $test."<br>";
}
}
echo contTest();
I have just used continueif element is equal to 1, meaning that element will be
skipped (ignored).
The output is :
我刚刚使用continueif element is equal to 1,这意味着该元素将被跳过(忽略)。输出是:
Got here
Got here
3
Got here
4
Got here
5
As you can see 1 is ignored.
如您所见,1 被忽略。
回答by softcr
The continuebasically says ignore the rest of the code after continueand start with the next step of the foreach loop. So the result you are getting is perfectly okay (see http://www.php.net/manual/de/control-structures.continue.php). There must be some other effect that changed your output.
该continue基本上说忽略其余代码后continue,并与foreach循环的下一个步骤开始。所以你得到的结果是完全没问题的(见http://www.php.net/manual/de/control-structures.continue.php)。一定有其他一些影响改变了你的输出。
回答by Kamal
This is what continue does exactly:
这正是 continue 所做的:
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. -- http://www.php.net/manual/en/control-structures.continue.php
continue 在循环结构中用于跳过当前循环迭代的其余部分,并在条件评估和下一次迭代开始时继续执行。-- http://www.php.net/manual/en/control-structures.continue.php

