foreach and Loop Control
Iterate arrays with foreach and use break and continue.
foreach: The Array Loop
foreach is the idiomatic way to iterate over arrays and objects in PHP — no need to track an index manually.
foreach on Indexed Arrays
Iterate over values in an indexed array:
<?php
$fruits = ['apple', 'banana', 'cherry'];
foreach ($fruits as $fruit) {
echo $fruit . PHP_EOL;
}
// apple
// banana
// cherryAll lessons in this course
- if, elseif, and else
- Switch and Match Statements
- for and while Loops
- foreach and Loop Control