php 突破 if 和 foreach

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9215588/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:22:17  来源:igfitidea点击:

break out of if and foreach

phpif-statementforeachbreak

提问by au_stan

I have a foreach loop and an if statement. If a match is found i need to ultimately break out of the foreach.

我有一个 foreach 循环和一个 if 语句。如果找到匹配项,我需要最终退出 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>
    }       
}

回答by Kaii

ifis not a loop structure, so you cannot "break out of it".

if不是循环结构,因此您无法“摆脱它”。

You can, however, break out of the foreachby simply calling break. In your example it has the desired effect:

但是,您可以foreach通过简单地调用break. 在您的示例中,它具有所需的效果:

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..

只是为了其他人偶然发现这个问题寻找答案的完整性..

breaktakes an optional argument, which defines how manyloop structures it should break. Example:

break接受一个可选参数,它定义它应该中断多少个循环结构。例子:

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 !

1 3 2 1 !

回答by Decent Dabbler

foreach($equipxml as $equip) {
    $current_device = $equip->xpath("name");
    if ( $current_device[0] == $device ) {
        // found a match in the file            
        $nodeid = $equip->id;
        break;
    }
}

Simply use break. That will do it.

只需使用break. 这样就可以了。

回答by serraosays

A safer way to approach breaking a foreachor whileloop in PHP is to nest an incrementing counter variable and ifconditional inside of the original loop. This gives you tighter control than break;which can cause havoc elsewhere on a complicated page.

在 PHP 中打破foreachorwhile循环的一种更安全的方法是if在原始循环中嵌套一个递增的计数器变量和条件。这为您提供了比break;在复杂页面上的其他地方造成严重破坏的更严格的控制。

Example:

例子:

// Setup a counter
$ImageCounter = 0;

// Increment through repeater fields
while ( condition ):
  $ImageCounter++;

   // Only print the first while instance
   if ($ImageCounter == 1) {
    echo 'It worked just once';
   }

// Close while statement
endwhile;

回答by Nick Constantine

For those of you landing here but searching how to break out of a loop that contains an include statement use return instead of break or continue.

对于那些登陆这里但正在寻找如何跳出包含 include 语句的循环的人,请使用 return 而不是 break 或 continue。

<?php

for ($i=0; $i < 100; $i++) { 
    if (i%2 == 0) {
        include(do_this_for_even.php);
    }
    else {
        include(do_this_for_odd.php);
    }
}

?>

If you want to break when being inside do_this_for_even.php you need to use return. Using break or continue will return this error: Cannot break/continue 1 level. I found more details here

如果你想在 do_this_for_even.php 中中断,你需要使用 return。使用中断或继续将返回此错误:无法中断/继续 1 级。我在这里找到了更多细节