Skip to content

Exercise 8 of 10 · Arrays

Packing List

What you will make

A packing list printed one item per line from an array, using foreach instead of a separate echo for every item.

The one new idea: an array holds several values, and foreach walks through them

Real data rarely comes as a single value — it's a list of items, rows or records. An array is PHP's way of holding many values together, and foreach is how you do the same thing to each one without writing it out by hand.

Go straight to the code ↓

Arrays and foreach

An array holds several values together, in order, under a single variable name — $items = ["tent", "rope", "map"] is one variable holding three values, not three separate variables. foreach is the loop built for walking through an array: foreach ($items as $value) runs its body once per item, and on each pass $value holds whatever item the loop has currently reached.

Whatever name you choose after as is the name you have to use inside the loop to reach the current item — PHP doesn't create any other name for you, and a different name inside the braces refers to something that was never set. The array itself doesn't change size or order just because you loop over it; foreach simply visits each existing value once, from the first to the last.

A worked example

Here's a different array, so you can see the pattern with fresh data:

PHP
<?php
$colors = ["red", "green", "blue"];

foreach ($colors as $color) {
    echo "Paint: $color\n";
}

Running it prints:

Output
Paint: red
Paint: green
Paint: blue

$colors holds three values. foreach visits each one in turn, and on every pass $color refers to whichever value the loop has reached — first "red", then "green", then "blue" — without needing to know in advance how many there are.

Your turn

The starter program is supposed to print every item in a packing list, one per line. Right now, nothing useful prints, or PHP warns about a variable that doesn't exist.

Look at the name given after as in the foreach line, and compare it to the name used inside the loop's echo statement. They need to be the exact same name. Fix the one inside the loop so it matches, then press Run.

If something goes wrong

If the output has blank items, or PHP warns about an undefined variable, the name inside the loop almost certainly doesn't match the name declared after asforeach only ever creates the one name you gave it there. If only one item prints instead of the whole list, check that the echo is indented inside the loop's { } braces, not sitting after the closing one.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Using a different variable name inside the loop than the one declared in foreach
foreach ($array as $x) only creates $x — using any other name inside the braces refers to a variable that was never set, which is usually empty rather than an error you'll immediately notice.
Writing an index-based for loop instead of foreach for a plain list
for ($i = 0; $i < count($items); $i++) works but needs $items[$i] to reach each value. foreach hands you the value directly and is the more natural fit when you don't need the index.
Forgetting the square brackets when creating the array
$items = "tent", "sleeping bag"; is not a valid array — a PHP array literal needs square brackets around the whole list: $items = ["tent", "sleeping bag"];.

Want a blank editor instead? Open the PHP playground.