Quantcast
Channel: User andrewsi - Stack Overflow
Viewing all articles
Browse latest Browse all 51

Answer by andrewsi for loop failing to break out upon validation

$
0
0

break will break out of the loop that you're currently in; a simplified version of your code is:

for ($x = 0; $x <= 5; $x++){
    foreach($decoded as $value) {
        if(!empty($ipdv6)){
            break;
        }
    }
}

So your break will exit the foreach loop, returning control to the outer for loop.

The manual page for break says that it can take an optional parameter, which is how many levels of loop to break out of; in this case:

for ($x = 0; $x <= 5; $x++){
    foreach($decoded as $value) {
        if(!empty($ipdv6)){
            break 2;
        }
    }
}

will break out of both loops.

Bear in mind that doing this makes your code that much more difficult to maintain - I'm a big fan of writing code that I can understand quickly when I come back to re-read it six months later, and this isn't something I find especially intuitive.


Viewing all articles
Browse latest Browse all 51

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>