I wrote a piece of tutorial code and ran into something quite strange after running it.
My Chrome extension Var Dumpling didn't see the last entry in the array because an ampersand had been appended to the type of the value.
I tested with this piece of code:
$alphabet = array('a', 'b', 'c');
foreach ($alphabet as &$letter) {
$letter .= 'a';
}
var_dump($alphabet);
The result of the var_dump is:
array(3) {
[0]=>
string(2) "aa"
[1]=>
string(2) "ba"
[2]=>
&string(2) "ca"
}
You can see that the last entry is &string(2) "ca"
instead of what I would expect string(2) "ca"
. There is no problem in the logic part of this, I can call $alphabet[2]
and it would return the value of the last entry ca
.
What I am wondering is, is this convention or some weird hickup in PHP?