Basic form
foreach ($items as $item) visits each value. Use foreach ($items as $key => $value) when you also need each key. Put the work for one iteration inside braces.
Empty input
An empty array produces no iterations. Initialize accumulators before the loop so they still have useful values after an empty input.
A common mistake
A return inside the body ends the surrounding function. When calculating a result from every value, place the final return after the closing brace. This reference uses iteration by value; references require additional care and are covered in the official manual.
Tested examples
$total = 0; foreach ([2, 3, 4] as $number) { $total += $number; } echo $total;9