Some searching through Google (and my own experience) shows that in PHP you can’t grab an array element when it’s been returned from a function call on the same line. For example, you can’t do:
echo getArray()[0];
However, I’ve come across a neat little trick:
echo ${!${false}=getArray()}[0];
It actually works. Problem is, I don’t know why it works. If someone could explain, that would be great.
Thanks.
This is how it works, step by step
assigns the result of getArray to a variable with an empty name (” or null would work instead of false)
negates the above value, turning it to boolean false
converts the previous (false) value to an (empty) string and uses this string as a variable name. That is, this is the variable from the step 1, equal to the result of getArray.
takes index of that “empty” variable and returns an array element.
Some more variations of the same idea
As to why
getArray()[0]doesn’t work, this is because php team has no clue how to get it to work.