PHP break out of an inner loop 

Joined:
03/10/2011
Posts:
84

May 16, 2011 20:55:53    Last update: May 16, 2011 20:55:53
The PHP break statement takes an optional level parameter, which indicates how many levels of loop structure to break out.

Example code:
<?php
for ($i = 0; $i < 5; $i++) {
    for ($j = 0; $j < 10; $j++) {
	if ($j > 5) break 2; // break out of the outer loop, 2 levels up
	echo "\$j: $j\n";
    }
}
?>


It's a fatal error if the level parameter exceeds the maximum number of enclosing loop structures. For example, "break 3;" in above code.
Share |
| Comment  | Tags