break
(PHP 4, PHP 5, PHP 7, PHP 8)
break
는 for
, foreach
, while
, do-while
또는 switch
구조의 현재 실행을 종료합니다.
break
는 얼마나 많은 중첩된 둘러싸는 구조에서 분리되어야 하는지 알려주는 선택적 숫자 인수를 허용합니다. 기본값은 1
이며 바로 둘러싸는 구조만 분리됩니다.
<?php
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
foreach ($arr as $val) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
/* Using the optional argument. */
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>