PHP Arrays
Loop through an indexed array.
PHP
<?php
$scores = [82, 91, 76];
foreach ($scores as $score) {
echo "$score\n";
}Output
82 91 76
Explanation
An indexed array preserves order. foreach assigns each item to $score before executing the loop body.
Explore the dry run
Guided walkthroughReady
1
<?php2
$scores = [82, 91, 76];3
foreach ($scores as $score) {4
echo "$score\n";5
}Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Create a three-element indexed array. | $scores = [82, 91, 76] | — | |
| foreach assigns each value to $score. | $scores = [82, 91, 76] | — | |
| Echo each score and a newline. | $scores = [82, 91, 76] | 82 91 76 |