$array = array(1,2,3); foreach ($array as $item){ if ($item == 2) { break; } echo $item; }
prints "1" because the loop was broken forever until the echo could print "2".
$array = array(1,2,3); foreach ($array as $item){ if ($item == 2) { continue; } echo $item; }
outputs 13 because the second iteration passed
$numbers = array(1,2,3); $letters = array("A","B","C"); foreach ($numbers as $num){ foreach ($letters as $char){ if ($char == "C") { break 2;
infers AB due to break 2 , which means that both statements were broken pretty early. If it was just break , the output would be AB1AB2AB3 .
$numbers = array(1,2,3); $letters = array("A","B","C"); foreach ($numbers as $num){ foreach ($letters as $char){ if ($char == "C") { continue 2; } echo $char; } echo $num; }
ABABAB outputs, due to continue 2 : each loop will be transmitted by an outer loop.
In other words, continue stops the current iteration, but allows another to run, and break stops the whole statement completely.
Thus, we can use that continue is only applicable for loops, while break can be used in other statements, such as switch .
A number represents the number of dependent nested .
if there are 2 nested loops, break in the inner will break the inner inner (however, this makes little sense, since the inner loop will start again in the next iteration of the outer loop). break 2 in the inner loop will break both.