打破 if 和 foreach

发布于 2025-01-03 21:50:07 字数 338 浏览 1 评论 0原文

我有一个 foreach 循环和一个 if 语句。如果找到匹配项,我需要最终摆脱 foreach 和 if 。

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唱一曲作罢 2025-01-10 21:50:07

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

但是,您可以通过简单地调用 break。在您的示例中,它具有预期的效果:

$device = "wanted";
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 immediately and also the if statement
        break;
        some_function(); // never reached!
    }
    another_function();  // not executed after match/break
}

只是为了让其他偶然发现此问题并寻找答案的人完整。.

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

foreach (['1','2','3'] as $a) {
    echo "$a ";
    foreach (['3','2','1'] as $b) {
        echo "$b ";
        if ($a == $b) { 
            break 2;  // this will break both foreach loops
        }
    }
    echo ". ";  // never reached!
}
echo "!";

结果输出:

1 3 2 1!

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

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

$device = "wanted";
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 immediately and also the if statement
        break;
        some_function(); // never reached!
    }
    another_function();  // not executed after match/break
}

Just for completeness for others who stumble upon this question looking for an answer..

break takes an optional argument, which defines how many loop structures it should break. Example:

foreach (['1','2','3'] as $a) {
    echo "$a ";
    foreach (['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 !

冷弦 2025-01-10 21:50:07
foreach($equipxml as $equip) {
    $current_device = $equip->xpath("name");
    if ( $current_device[0] == $device ) {
        // found a match in the file            
        $nodeid = $equip->id;
        break;
    }
}

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

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.

幸福丶如此 2025-01-10 21:50:07

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

例子:

// 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;

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

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;
冷弦 2025-01-10 21:50:07

对于那些登陆此处但正在搜索如何跳出包含 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);
    }
}

?>

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文