break是结束整个循环体,continue是结束单次循环
break n是结束外层第n层整个循环体,continue n是结束外层第n层单次循环
//例1
for($i=2;$i>0;$i--){
for($j=0;$j<2;$j++){
for($k=10;$k<14;$k++){
if($k>12) continue 2;
echo $k;echo " ";
}
}
}
//10 11 12 10 11 12 10 11 12 10 11 12
//例2
$data = [
['sex' => 'feman', 'name' => 'zs'],
['sex' => 'man', 'name' => 'ls'],
['sex' => 'feman', 'name' => 'ww']
];
foreach ($data as $val) {
switch ($val['sex']) {
case 'man' : continue 2;break;
case 'feman' : var_dump($val);echo "<hr>";break;
}
}
// array(2) { ["sex"]=> string(5) "feman" ["name"]=> string(2) "zs" }
// array(2) { ["sex"]=> string(5) "feman" ["name"]=> string(2) "ww" }
//由此看出,switch也被看作循环