In PHP, what are some alternative approaches to terminating a loop after a certain number of iterations besides using a counter variable?
Using a counter variable to terminate a loop after a certain number of iterations can sometimes be cumbersome or inefficient. An alternative approach is to use a `foreach` loop with a limited array of items to iterate over, or to use the `break` statement to exit the loop when a specific condition is met.
// Using a foreach loop with a limited array
$items = [1, 2, 3, 4, 5];
$limit = 3;
foreach ($items as $item) {
echo $item . "\n";
$limit--;
if ($limit <= 0) {
break;
}
}