Access by Offset with [ ]

Each item in an array is numbered starting from 0. For example, when we create an array:

$myArray = array("do", "re", "mi");

Each item is numbered starting from 0, like this:

| "do" | "re" | "mi" |
   0      1      2      

The item "do" is in position 0, the item "re" is in position 1, and so on.

Therefore, we can access a particular item of the array using its position, like this:

$myArray = array("do", "re", "mi");

echo $myArray[0]
// outputs "do"
  1. First we create an array named $myArray
  2. Then we use echo to output the first item in $myArray. Since items are numbered starting from 0, "do" is at position 0.

In line 8, there's an array named $tens. Use echo to output the third item in $tens. Remember that items are numbered starting from 0.

Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

Since items in an array are numbered starting from 0, the third item is at position 2, like this:

echo $tens[2];

You can use echo and print interchangeably.

View Preview