×

php · level 1 · unit 6 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Every item in an array has an , starting at 0.

Every item has a place number called its index. PHP counts from 0, so the first item is $queue[0] and the second is $queue[1]. Picture a queue where the person at the front is number 0.

Put a new value at an index to replace that item: $queue[1] = "Bea";. Empty square brackets add a new item on the end: $queue[] = "Dan";.

Ask for an index with no item and PHP stops with Undefined array key. The last item is always at count($queue) - 1.

<?php
$queue = ["Ava", "Ben", "Cleo"];
echo $queue[0] . " is first\n";
$queue[1] = "Bea";
$queue[] = "Dan";
echo implode(", ", $queue) . "\n";

The index always goes in square brackets, straight after the array's name.