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;
/* ここでは、'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

User Contributed Notes

There are no user contributed notes for this page.
To Top