Php break foreach inside if

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 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 [['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 !

[PHP 4, PHP 5, PHP 7, PHP 8]

break ends execution of the current for, foreach, while, do-while or switch structure.

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.

ei dot dwaps at gmail dot com

1 year ago

You can also use break with parentheses: break[1];

Note:
Using more nesting level leads to fatal error:

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Using break keyword: The break keyword is used to immediately terminate the loop and the program control resumes at the next statement following the loop. To terminate the control from any loop we need to use break keyword. The break keyword is used to end the execution of current for, foreach, while, do-while or switch structure. But in nested loops, to exit out from all or some of the outer loops, we need to pass a numeric argument which tells it how many nested enclosing structures are to be terminated.

    Syntax:

    break number_of_nested_loop_to_terminate;

    Parameters: The break keyword followed by a numeric argument which is by default 1. Passing of variables and 0 as a numeric argument is not allowed.

    Examples:

    break 2; // It terminates second outer loop
    break 3; // It terminates third outer loop
    break $num; // Variable cannot be used as numeric argument since version 5.4.0
    break 0; // 0 is not a valid argument to pass
    

    Below programs illustrate how to break outer loops in PHP:

    Program 1: PHP program to display the number while inner loop is not terminated.

    Program 2: PHP program to search a number in an array and break the outer loop when it is found.

    Output:

    45 is found in the array
    

    Using goto keyword: The goto keyword is used to jump the section of the program. It jumps to the target label.

    Program 3: PHP program to break the outer loop using goto keyword.

    Output:

    45 is found in the array
    


    Can I use break in foreach PHP?

    To terminate the control from any loop we need to use break keyword. The break keyword is used to end the execution of current for, foreach, while, do-while or switch structure.

    How do you break a foreach loop with conditions?

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

    How do you write if else in foreach?

    forEach[element => { if[element. key === 'first'] { // do something } else if[element. key === 'second'] { // do something } else { // do something else } }]; This will do pretty much exactly what you expect.

    Can I use continue in foreach PHP?

    Introduction. The continue statement is one of the looping control keywords in PHP. When program flow comes across continue inside a loop, rest of the statements in current iteration of loop are skipped and next iteration of loop starts. It can appear inside while, do while, for as well as foreach loop.

    Chủ Đề