I initially thought; it prints $int to a string because [] is another way to access string positions. It seems plausible that this will not cause an error:
$string 'Hello'; echo $string[0];
However, this is not the case:
$int = 1; echo $int[0];
Prints nothing, and var_dump($int[0]) returns NULL .
Interestingly, the same behavior appears for bool and float , and the operation used is FETCH_DIM_R , which represents the value of an element in "index" in "array-value" to save it in the "result" ".
From the Arrays guide:
Note The array dereferences a scalar value that is not a string and gives NULL , i.e. without giving an error message.
Like this phenomenon where an error does not occur. Errors # 68110 and # 54556 :
$array['a'] = null; echo $array['a']['non-existent'];
Not surprisingly, the assignment does not work:
$int = 1; $int[0] = 2;
Warning: you cannot use a scalar value as an array
Thus, PHP is actually trying to access int , bool , float as an array, but does not generate an error. This is from at least version 4.3.0 to 7.2.2.
Abracadaver
source share