A safer way to approach breaking a foreach
or while
loop in PHP is to nest an incrementing counter variable and if
conditional inside of the original loop. This gives you tighter control than break;
which can cause havoc elsewhere on a complicated page.
Example:
// Setup a counter
$ImageCounter = 0;
// Increment through repeater fields
while ( condition ):
$ImageCounter++;
// Only print the first while instance
if ($ImageCounter == 1) {
echo 'It worked just once';
}
// Close while statement
endwhile;