How to Break an Outer Loop with PHP

How can I break an outer loop with PHP?

In the case of 2 nested loops:

break 2;

http://php.net/manual/en/control-structures.break.php

PHP break from 2 loops

You can specify how many loop you want to break that way :

break 2;

So in your case :

while(true) {
foreach($return AS $row) {
if($row['timer'] > 15){
break 2;
}
}
sleep(2);
}

Breaking the nested loop

Unlike other languages such as C/C++, in PHP you can use the optional param of break like this:

break 2;

In this case if you have two loops such that:

while(...) {
while(...) {
// do
// something

break 2; // skip both
}
}

break 2 will skip both while loops.

Doc: http://php.net/manual/en/control-structures.break.php

This makes jumping over nested loops more readable than for example using goto of other languages

php - Nested Loop, Break Inner Loops and Continue The Main Loop

After reading the comment to this question (which was deleted by its author) and did a little research, I found that there is also parameter for continue just like break. We can add number to the continue like so:

while($something) {

foreach($array as $value) {
if($ok) {
continue 2;
// continue the while loop
}

foreach($value as $val) {
if($ok) {
continue 3;
// continue the while loop
}
}
}
}

how to break children loops only

I used this and its worked

             for ($i = 0; $i < 20; $i++) {
// some codes
for ($i1 = 0; $i1 < 10; $i1++) {
if (a condition)
{
$i1=11;
}
}
// some codes

}

In Php 'foreach' skip the current iteration block and also rest of the loop

Try break 2;

If you want to exit out of nested loops you have to use the "argument" for break.

foreach ($column_names as $heading) {
foreach ($heading as $column_heading)
if($column_heading == "trip_id"){
break 2; //break out of both loops.
}
if($column_heading == "number_of_pessengers"){
$column_heading = "No. pessengers";
}
$cellWidth = $pdf->GetStringWidth($column_heading);
$pdf->Cell($cellWidth + 2, 10, $column_heading, 1);
}
}

Who knew you can put a number with break, also continue works the same way.

http://php.net/manual/en/control-structures.break.php

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.

Cheers

Break loop from inside loop php

In your code:

 $alp[$hit++] = $alp[$i].$alp[$i2];
// $hit is already increased.
// Therefore `$alp[$hit]` does not exist
if($alp[$hit] == "AM"){
break;
}

Replace with something like:

 $alp[$hit] = $alp[$i].$alp[$i2];
// $hit is still the same
if($alp[$hit] == "AM"){
// `break` will stop inner `for` loop (with $i2)
break;
// use `break 2;` to break both `for` loops
}
$hit++;

Nested foreach loop, break inside loop

Can you check something like this:

function create_event($numbers,$available_dates) {
foreach ($numbers as $number) {
foreach ($available_dates as &$av_date) {
if (dateCheck($av_date, $number) == 0) {
unset($av_date);
break;
}
}
}
}

What's the best way to break from nested loops in JavaScript?

Just like Perl,

loop1:
for (var i in set1) {
loop2:
for (var j in set2) {
loop3:
for (var k in set3) {
break loop2; // breaks out of loop3 and loop2
}
}
}

as defined in EMCA-262 section 12.12. [MDN Docs]

Unlike C, these labels can only be used for continue and break, as Javascript does not have goto.



Related Topics



Leave a reply



Submit