php break foreach loop code example
Example 1: php exit foreach
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
foreach ($arr as $val) {
if ($val == 'stop') {
break;
}
echo "$val<br />\n";
}
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1;
case 10:
echo "At 10; quitting<br />\n";
break 2;
default:
break;
}
}
?>
Example 2: foreach loop in php
<?php
$arr = ['Item 1', 'Item 2', 'Item 3'];
foreach ($arr as $item) {
var_dump($item);
}
$dict = array("key1"=>"35", "key2"=>"37", "key3"=>"43");
foreach($dict as $key => $val) {
echo "$key = $val<br>";
}
?>