CKTIS / PHP
Keyword break
Leave the nearest loop or switch immediately.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
break 2;Leave the nearest loop or switch immediately. A positive literal level counts enclosing loops and switches, so break 2 exits both nested loops in this search. Levels exceeding the actual nesting are rejected. Processing resumes after the selected structure.
$seen = [];
foreach ([[1,2],[3,4]] as $pair) { foreach ($pair as $v) { $seen[]=$v; if ($v===2) { break 2; } } }
echo json_encode($seen);
[1,2]
Examples
<?php
$seen = [];
foreach ([[1,2],[3,4]] as $pair) { foreach ($pair as $v) { $seen[]=$v; if ($v===2) { break 2; } } }
echo json_encode($seen);
Result
[1,2]