|
Posted by ljb on 04/24/07 00:49
I was a bit surprised to find that indexing some non-arrays as if they were
arrays does not even raise a Notice message. This works for NULL, integer,
and floats at least. (PHP-5.2.1)
error_reporting(E_ALL+E_STRICT);
$arry = array(1=>1);
$nullvar = NULL;
$intvar = 1;
echo $arry[1]; # Echos 1
echo $arry[2]; # Notice: undefined offset.
echo $nullvar[1]; # Echos nothing and no Notice.
echo $intvar[0]; # Ditto
echo $intvar[1]; # Ditto
The manual says that if you convert integer to array, you get an array with
one element at [0]. But that isn't what is happening here, because
$intvar[0] is NULL, not 1, and $intvar[1] does not result in an undefined
offset notice. Instead, I get an array with NULL for any index.
I actually find this behavior useful, but I don't like depending on what
seems to be a quirk.
[Back to original message]
|