I believe you removed the code from here . And I also believe that you have not finished reading the document, because at the end all the answers are shown, which in this case corresponds to letter D , but let's go by parts.
The correct answer is the letter D . You can see - as stated in the PHP documentation , - that an array is an ordered data structure representing a collection of elements, and can be started in this way. Then on the first line :
$a = array("a", "b", "c", "d");
You indicate that $a
is an array that will have 4 positions, with 4 other indexes (0 ... 3), being interpreted as follows:
array (size=4)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'd' (length=1)
In the second line , you instruct PHP to add, in the last valid position of the array string p>
$a[] = "e";
Soon $a
will contain all the letters 'a', 'b', 'c', 'd', 'e'.
array (size=5)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'd' (length=1)
4 => string 'e' (length=1)
When you can not access the array , making echo $a
, it is because $a
is a non-scalar, composite type, other than a simple string . In other words, you need something more to manipulate arrays .
Since you did not create the array with the positions manually, the array followed the pattern and added the data, always after the last valid position. So to access the array $a
, you should do something similar to this:
echo $a[posição];
In case, as your array , has a size of 5, you can access any of the positions (starting from 0 to 4), as follows:
echo $a[0]; // imprime a
If you try to do something like echo $a
, a notice will be generated, saying: Array to string conversion . Read more about this here .