PHP breaks from 2 loops

I have a problem with breaking from loops. I have code like this:

<?php
$return = array(...);
while(true) {
    foreach($return AS $row) {
        if($row['timer'] > 15)
            break;
    }
    sleep(2);
}

      

And I need to break while (true)

+3


source to share


3 answers


You can specify how many loops you want to split this way:

break 2;

      



So in your case:

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

      

+13


source


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

      



0


source


You can try something like this:

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

      

0


source







All Articles