break

break는 현재 for, foreach, while, do-while 또는 switch절의 수행을 멈춘다.

break는 숫자 인수 옵션을 허용함으로써 내포된 구문구조의 깊이를 표시하고 거기서 빠져나올수 있게 해준다.

<?php
$arr 
= array('one''two''three''four''stop''five');
while (list (, 
$val) = each ($arr)) {
    if (
$val == 'stop') {
        break;    
/* 여기서는 'break 1;'으로 슬 수 있습니다. */
    
}
    echo 
"$val<br />\n";
}

/* 옵션 인수 사용하기. */

$i 0;
while (++
$i) {
    switch (
$i) {
    case 
5:
        echo 
"At 5<br />\n";
        break 
1;  /* switch만 빠져나갑니다. */
    
case 10:
        echo 
"At 10; quitting<br />\n";
        break 
2;  /* switch와 while을 빠져나갑니다. */
    
default:
        break;
    }
}
?>

add a note add a note

User Contributed Notes 2 notes

up
4
ei dot dwaps at gmail dot com
3 years ago
You can also use break with parentheses: break(1);

Note:
Using more nesting level leads to fatal error:

<?php
while (true) {
    foreach ([
1, 2, 3] as $value) {
      echo
'ok<br>';
      break
3; // Fatal error: Cannot 'break' 3 levels
   
}
    echo
'jamais exécuter';
    break;
  }
?>
up
-4
mparsa1372 at gmail dot com
3 years ago
The break statement can also be used to jump out of a loop.

This example jumps out of the loop when x is equal to 4:

<?php
for ($x = 0; $x < 10; $x++) {
  if (
$x == 4) {
    break;
  }
  echo
"The number is: $x <br>";
}
?>
To Top