Arrays and Iteration
Foreach Values
foreach visits each value in an array without manual index arithmetic.
Foreach Values
foreach_values.php
<?php
$bonus = ;
$total = 0;
foreach ([4, 6, 8] as $score) {
$total = $total + $score + $bonus;
}
echo "bonus=" . $bonus . "\n";
echo "total=" . $total . "\n";
<?php
$bonus = ;
$total = 0;
foreach ([4, 6, 8] as $score) {
$total = $total + $score + $bonus;
}
echo "bonus=" . $bonus . "\n";
echo "total=" . $total . "\n";
<?php
$bonus = ;
$total = 0;
foreach ([4, 6, 8] as $score) {
$total = $total + $score + $bonus;
}
echo "bonus=" . $bonus . "\n";
echo "total=" . $total . "\n";
foreach loop
A `foreach` loop assigns each array value to a loop variable for one pass through the block.