#break跳出多层循环 $a = 0; while (true) { $a++; switch ($a) { case 1: echo ‘1‘; break; case 2: echo ‘2‘; break; default: echo ‘in‘; break 2; } } echo ‘out‘;
输出:1 2 in out
说明:
- break除了可以在switch里面用,还可以从for、while等循环语句中跳出;
- break+数字可以支持多重循环的跳出。
#continue跳出多重循环 $i = 0; while ($i++ < 5) { echo "Outer<br />\n"; while (1) { echo "Middle<br />\n"; while (1) { echo "Inner<br />\n"; continue 3; } echo "This never gets output.<br />\n"; } echo "Neither does this.<br />\n"; }
输出:
Outer
Middle
Inner
Outer
Middle
Inner
……
输出5遍
说明:
- continue可以跳出多重循环
- continue语句作用到 switch 上的作用类似于 break
#switch语句 $i = 0; switch ($i) { case 0: case 1: case 2: echo "i的值为0或1或2"; break; default: echo "i的值不为0或1或2"; }
输出:i的值为0或1或2
说明:
- switch中在一个 case 中的语句可以为空,这样只不过将控制转移到了下一个 case 中的语句
declare(ticks=1); // A function called on each tick event function tick_handler() { echo "tick_handler() called\n"; } register_tick_function(‘tick_handler‘); $a = 1; if ($a > 0) { $a += 2; print($a); }
输出:
tick_handler() called
tick_handler() called
tick_handler() called
3
tick_handler() called
tick_handler() called
tick_handler() called
说明:
declare(ticks)语法记录定义的语句个数,每执行一个定义语句由register_tick_function函数定义的函数执行一次(可以用来记数)
时间: 2024-10-05 23:36:42