Arrays

An array is the first collection of PHP: one name for an ordered list of values, read by position or walked in a loop.

Create an array

An array stores an ordered list of values under one name. The short [] syntax builds one in a single expression, and positions count from zero. count() tells how many elements the array holds. The companion page Arrays in PHP shows more array forms.

<?php $colors = ["red", "green", "blue"]; $empty = []; var_dump(count($colors));

php create.php

int(3)

The empty array holds nothing yet, and the three colours are numbered 0, 1 and 2, so the count is 3.

Read by offset

Square brackets read one element by its position: $colors[0] is the first item, $colors[2] the third. Reading a missing offset raises a warning and evaluates to null, so check long-lived data with isset first.

<?php $colors = ["red", "green", "blue"]; echo $colors[0], PHP_EOL; echo $colors[2], PHP_EOL;

php read.php

red
blue

The offset 1 holds "green" and is skipped here, which shows that the position, not the order of the echoes, chooses the element.

Append elements

Empty brackets append after the last element: $cart[] = "book" adds one more item and numbers it automatically. This is how lists grow one element at a time, for example while reading rows or collecting input.

<?php $cart = []; $cart[] = "book"; $cart[] = "pen"; echo count($cart), PHP_EOL;

php add.php

2

The two appends gave the items the positions 0 and 1, and the count after them is 2. No number is written in the code, because the next free position comes from the array itself.

Loop over arrays

The foreach loop visits every element in order and needs no counter. A classic for loop with an index works too, and fits when the position itself drives the logic.

<?php $cart = ["book", "pen"]; foreach ($cart as $item) { echo $item, PHP_EOL; } for ($i = 0; $i < count($cart); $i++) { echo $cart[$i], PHP_EOL; }

php loop.php

book
pen
book
pen

The two loops print the same two items, the first without a counter and the second with an explicit $i that runs from 0 to the count minus one.

Exercises

  1. Add a fourth colour to create.php and print the new count. Which position does the new colour take?
  2. Print $colors[1] and $colors[3] in read.php and run it. What does PHP warn about, and what does the second line print?
  3. Append "ruler" to the cart after the loop in add.php and check the count again.
  4. Walk the cart with foreach and print the position of every item next to it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which position does the first element of a PHP array take?

    Answer

    0

    The keys of a list start at 0, so $colors[0] is the first element.

  2. What does $cart[] = "book"; do?

    Answer

    It appends the value at the next free position

    Empty brackets after the variable name append the value and give it the next integer key.

  3. What does reading a missing offset do?

    Answer

    It raises a warning and evaluates to null

    PHP 8 reports an undefined array key as a warning and the expression gives null, which is why isset comes first.

  4. Which function returns the number of elements of an array?

    Answer

    count()

    count() returns the number of elements; sizeof() is its alias, and the other two are not PHP functions.

  5. What does this program print? <?php $colors = ["red", "green", "blue"]; echo count($colors);

    Answer

    3

    The list holds three strings, so the count is 3.

  6. What does this program print? <?php $colors = ["red", "green", "blue"]; echo $colors[1];

    Answer

    green

    Offset 1 is the second element, because the positions start at zero.

  7. What does this program print? <?php $cart = []; $cart[] = "book"; $cart[] = "pen"; echo count($cart);

    Answer

    2

    Each empty-bracket assignment appends one element, so the cart holds two.

  8. What does this program print? <?php $cart = ["book", "pen"]; foreach ($cart as $item) { echo $item; }

    Answer

    bookpen

    The loop prints the two strings one after the other with no separator, so they run together.

  9. What does this program print? <?php $letters = ["a", "b", "c"]; echo $letters[2];

    Answer

    c

    The last element of a three-element list has offset 2, so the echo prints c.

  10. What does this program print? <?php $cart = ["book"]; $cart[] = "pen"; echo $cart[1];

    Answer

    pen

    The bracket assignment appends "pen" at offset 1, which the last line then reads.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev