break leaves a loop or switch at once; continue skips to the next pass. Both take a level count: break 2 leaves two nested loops, which replaces a "found" flag tested at every level.
<?php
$shelves = ['A' => ['Dune', 'Emma', 'Ulysses'], 'B' => ['Beloved', 'Neuromancer'],
'C' => ['Neuromancer', 'Walden']];
foreach ($shelves as $shelf => $titles) { // find the first copy, then stop
foreach ($titles as $pos => $title) {
if ($title === 'Neuromancer') { echo "found at $shelf$pos\n"; break 2; }
}
}
foreach ($shelves as $shelf => $titles) {
foreach ($titles as $title) {
if ($title === 'Beloved') continue 2; // abandon shelf B entirely
if (strlen($title) < 5) continue; // skip this title only
echo "$shelf:$title ";
}
}
echo "\n";Output
found at B1 A:Ulysses C:Neuromancer C:Walden
A switch counts as a loop level, so a bare continue in a switch inside a loop only leaves the switch, and PHP warns: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? Other idioms: while (true) with one clear break for read-until-sentinel loops, count() hoisted out of a for condition, and array_find() or array_any() (array_find and Friends) for plain searches.