This question already has an answer here:
This is just a simple question out of curiosity. I spent the whole day debugging my PHP code, and found the problem was due to treating an integer as an array:
$x = $int[$index]; // this returns null, but no error at all
The integer was actually meant to be an array, but the function that was meant to pass the array messed up and passed the first value in the array instead. Is there any reason why an error isn't shown? Accessing an undefined index on an array creates an error, however trying to access a non-existent index on an integer doesn't?
this is "type juggling", a "feature" of PHP.
you can read more in the official doc
EDIT According to documentation, "The behaviour of an automatic conversion to array is currently undefined."
I run a quick test, just for curiosity:
and the output shows that gettype() returns NULL, so I guess that, in this case, is converted to NULL
Contrary to my original theory that you're invoking undefined behavior, this behavior actually is defined in the Array documentation.
In that case, it seems like there's no type juggling happening at all, so these references to documentation regarding conversion to array aren't useful in understanding this.
Explicit conversion to array is defined.
Automatic conversion to array is undefined according to the type juggling documenation.
At first I thought this was what was happening in this case, but since salathe pointed out that this behavior is documented elsewhere, I'm not sure what "automatic conversion to array" is.
As to why this gives you a null value without throwing an error, warning, or notice, that's just how the PHP interpreter was implemented, and as far as why that is the case, it's not really answerable here. You'd have to ask the developers, and they might be able to tell you.
I originally thought; it typecasts
$int
to a string because[]
is another way of accessing string positions. Seems plausible that that wouldn't generate an error:However, that's not the case:
Outputs nothing and
var_dump($int[0])
returnsNULL
.Interestingly, the same behavior is exhibited for
bool
andfloat
and the operation used isFETCH_DIM_R
which is Fetch the value of the element at "index" in "array-value" to store it in "result".From the manual Arrays:
Similar to this phenomenon, where no error is generated. Bugs #68110 and #54556:
Not surprising that assigning doesn't work:
So PHP is actually attempting to access the
int
,bool
,float
as an array but not generating an error. This is from at least version 4.3.0 to 7.2.2.