I believe that you removed the code from here. And I also believe that you have not finished reading the document, because at the end is shown all the answers, which in this case corresponds to letter D, but let’s go in pieces.
The correct answer is the letter D. You can see - as stated in the PHP documentation, - that a array is an ordered data structure representing a collection of elements, and which can be initiated in this manner above. Then in the first line:
$a = array("a", "b", "c", "d");
You indicate that $a
is a array which will have 4 positions, with 4 other indices (0...3), and is then interpreted, thus:
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, at the last valid position of array, to string 'and':
$a[] = "e";
Soon $a
will contain all 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)
About you not being able to access array, making echo $a
, it is because $a
is a non-scale, composite type, different from a simple string. In other words, it takes something else to manipulate arrays.
How did you not create the array with the manual positions, the array followed the pattern and was adding the data, always after the last valid position. Then 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 at 0 until 4), as follows:
echo $a[0]; // imprime a
If you try to do something like echo $a
, a notice, saying: Array to string Conversion. Read more about this here.
not to echo an array so friendly, you must identify the position of the array to be printed so "echo $a[0]"
– Paz
all right, that’s what websites are for, we’re all here to learn and improve.
– Paz