|
Posted by Schraalhans Keukenmeester on 04/24/07 03:01
ljb wrote:
> 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.
If you convert an int to an array, the manual correctly says you end up
with a single element array containing its value. You're not doing that
here though. Try
$int=5;
$arr = (array) $int;
var_dump ($arr);
You are echoing values of a non-defined array-element, which PHP told
you, followed by some non-defined arrays. If you var_dump them, you'll
see they're NULL.
Nothing quirky about it in my book.
Sh.
[Back to original message]
|