Break Out of If and Foreach

break out of if and foreach

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 !

How to break out of a foreach once a condition is met?

From PHP documentation:

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

So yes, you can use it to get out of the foreach loop.

How to break out of each loop within an IF statement?

break; is something you can do in a while (...) or for (...)
loop, but you're inside a function. The proper way to exit a function
is to use return.

If you want the .each() loop to stop immediately, you should return false.

Source

How to break out from foreach loop in javascript

Use a for loop instead of .forEach()

var myObj = [{"a": "1","b": null},{"a": "2","b": 5}]
var result = false

for(var call of myObj) {
console.log(call)

var a = call['a'], b = call['b']

if(a == null || b == null) {
result = false
break
}
}

Early exit from function in a forEach?

Another approach, with for loop:

checkIfFollowed() {
for (let i = 0; i < this.currentUserInfos.followed.length; ++ i) {
if (18785 == 18785) {
console.log('its true');
this.alreadyFollowed = true;
return; // exit checkIfFollowed() here
}
}

this.alreadyFollowed = false;
console.log('the end');
return;
}

How to break out of the foreach loop at first result and continue loop from the 2nd result

laravel provides loop variables

Also when you use break it will jump out of a loop. So use continue instead of break

break and continue

      @php
$i = 0;
$len = count($gallery->galleryImages->where( 'gli_dimension', 'fullsize' ));
@endphp
@foreach( $gallery->galleryImages->where( 'gli_dimension', 'fullsize' ) as $img )
@if ($loop->first)
@php
$i++;
continue;
@endphp
@endif
<div style="display: none;">
<a href="{{ \Illuminate\Support\Facades\Storage::url( $img['gli_path']) }}"
data-fancybox="images-preview{{$gallery->gly_id}}"
data-width="1500" data-height="1000"
data-thumb="{{ \Illuminate\Support\Facades\Storage::url( $img['gli_path'] ) }}"
data-caption="{{ $gallery->gly_title }}"></a>
</div>
@endforeach

Laravel, is it possible to break if statement inside foreach?

I would use a do while inside the foreach

foreach($furniture as $fur){
//do something
do{
if($fur->broken == true){
//do something
break;
}
}while(false);
}

The Do While evaluates the condition after the first iteration, in this case the loop ends after the first iteration (because the condition is false), but this gives us a structure we can break out of.

The other way to do this is to use a switch statement with an if in it.

foreach($furniture as $fur){
//do something
switch(1){
default:
if($fur->broken == true){
//do something
break;
}
}
}

Also note to break out of the foreach you can do break 2 instead of just break.

All that said, I would say just from a best practice standpoint this makes the code less readable and what you need could be done without it ( most likely ) but I would need more details to help with that.

UPDATE

IS this a template thing like ( Blade )? It's not clear in the question. I don't use blade, but I've seen it, studied it some, so that makes sense with the @ sign. But as I took the time to type this I'll leave it here.

Of note the same could probably be done in blade by faking a loop ( loop 1 time ) inside the foreach, same principle as the do while. Assuming it supports break levels. Something like this

 @foreach($furniture as $fur)
//do something
@foreach($fake as $f)
@if($fur->broken == true)
//do something
@break
@endif
@endforeach
//do something else
@endforeach
//where fake is like $fake = [1]; single element, so one loop.

You could also do the extra loop inside or outside the if block, as your needs dictate.

I am also not sure if Blade supports switch most template engines don't, but it may be cleaner to use if it does.

How to break out of a ListT.ForEach() loop from within an if statement in C#

Use a return statement instead of continue. Remember that by using the ForEach extension method, you are executing a function for each item, the body of which is specified between { and }. By exiting the function it will just continue with the next value from the list.



Related Topics



Leave a reply



Submit