如果和foreach分离 [英] break out of if and foreach
问题描述
foreach($ equipxml as $ equip){
$ current_device = $ equip-> xpath(name );
if($ current_device [0] == $ device){
//找到文件中的匹配
$ nodeid = $ equip-> id;
<突破了if和foreach这里>
$ / code $ / pre
解决方案 code>如果
不是一个循环结构,所以你不能突破它。
您可以通过简单地调用 foreach www.php.net/breakrel =noreferrer> break
。在你的例子中,它具有所需的效果:
$ forex $ equipxml as $ equip
$ current_device = $ equip->的xpath( 名称);
if($ current_device [0] == $ device){
//找到文件中的匹配
$ nodeid = $ equip-> id;
//将离开foreach循环,if语句
break;
}
this_command_is_not_executed_after_a_match_is_found();
$ b $ p $为了完整性绊倒这个问题寻找答案。
break
接受一个可选的参数,它定义了它应该中断多少个循环结构。例子:
foreach(array('1','2','3')as $ a){
回显$ a;
foreach(array('3','2','1')为$ b){
echo$ b;
if($ a == $ b){
break 2; //这将破坏两个foreach循环
}
}
echo。; //永远不会达到
}
echo!;
结果输出:
$ b $如果 - 由于一些不明确的原因 - 如果 - 为了一些不明确的理由,你想break
出<$ c如果语句(这不是一个循环结构,因此每个定义不可破坏),你可以简单地把结构,所以你可以跳出代码块。
。
请注意,这是一个彻头彻尾的破解,通常你不会这样做:
do if($ foo)
{
//先做一些事情...
/ /我们应该继续这个区块,还是现在退出?
if($ abort === true)break;
//继续做某事...
} while(false);
上面的例子取自 /manual/en/control-structures.if.php#90073rel =noreferrer>>
语法:这是因为在这里使用缩写语法。如果($ foo){..}
$,那么外层大括号可以省略,因为循环结构只包含一个语句:b $ b
另一个例子:
do $ i ++; while($ i <100)
相当于do {$ i ++; } while($ i <100)
。I have a foreach loop and an if statement. If a match is found i need to ultimately break out of the foreach.
foreach($equipxml as $equip) { $current_device = $equip->xpath("name"); if ( $current_device[0] == $device ) { // found a match in the file $nodeid = $equip->id; <break out of if and foreach here> } }
解决方案
if
is not a loop structure, so you cannot "break out of it".You can, however, break out of the
foreach
by simply callingbreak
. In your example it has the desired effect:foreach($equipxml as $equip) { $current_device = $equip->xpath("name"); if ( $current_device[0] == $device ) { // found a match in the file $nodeid = $equip->id; // will leave the foreach loop and also the if statement break; } this_command_is_not_executed_after_a_match_is_found(); }
Just for completeness for others that stumble upon this question looking for an answer..
break
takes an optional argument, which defines how many loop structures it should break. Example:foreach (array('1','2','3') as $a) { echo "$a "; foreach (array('3','2','1') as $b) { echo "$b "; if ($a == $b) { break 2; // this will break both foreach loops } } echo ". "; // never reached } echo "!";
Resulting output:
1 3 2 1 !
If - for some obscure reason - you want to
break
out of anif
statement (which is not a loop structure and thus not breakable per definition), you can simply wrap yourif
in a tiny loop structure so you can jump out of that code block.Please note that this is a total hack and normally you would not want to do this:
do if ($foo) { // Do something first... // Shall we continue with this block, or exit now? if ($abort === true) break; // Continue doing something... } while (false);
The example above is taken from a comment in the PHP docs
If you wonder about the syntax: It works because an abbreviated syntax is used here. The outer curly braces can be left out because the loop structure contains only a single statement:
if ($foo) { .. }
.Another example for this:
do $i++; while ($i < 100)
is equivalent todo { $i++; } while ($i < 100)
.这篇关于如果和foreach分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!