break out of if and foreach
问题 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> } } 回答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: foreach(