PHP ‘break’ argument

I’m not sure if I ever knew this before, but the PHP break statement has an optional argument that declares how many levels to break out of, eg break 2. For instance, in the following example, the break will break out of both loops when the sub-item is found:

$theSubItem = null;
foreach(getItems() as $item){
    foreach($item->getSubItems() as $subItem){
            $theSubItem = $subItem;
            break 2;

ensuring that we won’t loop through any extra items or sub-items.

Published by


I am a quiet person from Northeast Ohio. I work as a web developer. I like computers, music, and many other things.

Leave a Reply

Your email address will not be published. Required fields are marked *