0Pricing
PHP Academy · Lesson

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
// cherry

All lessons in this course

  1. if, elseif, and else
  2. Switch and Match Statements
  3. for and while Loops
  4. foreach and Loop Control
← Back to PHP Academy