简体   繁体   中英

If Loop inside of While Loop causes infinite loop

I'm trying to loop through the first 5 items of an array that contain a specific value.

The code below causes an infinite loop.

$i = 0;
while ($i < 5):
    if ($counselor[$i]->state == $state || !$state): 
       // do stuff
       $i++; 
    endif;
endwhile;

Essentially I want to end the loop after the if statement has run 5 times.

While others have explained why your solution currently does not work, and some ways around it, the best alternative is to loop the entire array until you find 5 matches - by using a foreach -loop instead.

By using an foreach -loop, you will never run into issues if the array has less than 5 matching elements (if it has less than 5 matching elements, it will never break).

$i = 0;
// Loop the array
foreach ($counselor as $k=>$v) {
    // Check if there is a match
    if ($v->state == $state || !$state) {
        // Do whatever if a match here
        $i++;
    }
    // If we have found 5 matches, break out of the loop!
    if ($i == 5) {
        break;
    }
}

You can now check how big $i is, and if less than 5, you found less than 5 matches. If it's exactly 5, you found your matches, and ended the loop.

If the if statement is not met, then you will only stay checking the $counselor[0]->state .

You need a separate counter for when the if statement is met.

$i = 0;
$containsCount = 0;
while ($containsCount < 5 || !isset($counselor[$i])):

    if ($counselor[$i]->state == $state || !$state):
        // do stuff
        ++$containsCount;   
    endif;
    ++$i;

endwhile;

I've also added a bounds check by checking if the $counselor[$i] is null. (could also check $i < $arrayLength )

If your if statement is false the first time ($i = 0) it will never match...since $i always will be 0. The same goes for $i = 1, 2, 3 or 4. If any of those are false, the loop will be stuck. $i will never increase. You need another solution.

You increment $i only when the if statement returns true. If you take your $i++; out of the if statement the loop will always run exactly 5 times.

$i = 0;
while($i < 5):

    if($counselor[$i]->state == $state || !$state): 
       // do stuff
    endif;
    $i++; 

endwhile;

You have to exit $i++ of your if condition :

$i = 0;
while($i < 5):

    if($counselor[$i]->state == $state || !$state): 
       // do stuff
    endif;
    $i++; 

endwhile;

if the if() condition is not met 5 times, it will be infinite as the while() condition will never be met. Instead of while statement, use a loop that matches the length of the array and it will end then when it reaches the end of the array.

Maybe you can try altering your if statement a bit like this :

$i = 0;
while($i < 5):

    if($counselor[$i++]->state == $state || !$state): 
       // do stuff
    endif;

endwhile;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM