开发者

php array access on boolean

I recently stumbled across the following:

<? $d=false; var_dump($d[123]); ?>

which yielded NULL, but (unexpected to me) without any notice, as for example

<? $d=array(开发者_开发技巧); var_dump($d[123]); ?>

does produce the well known

Notice: Uninitialized string offset:  123 in - on line 1

What is going on here? Is there any documentation of this behavior?


From here:

Accessing variables of other types (not including arrays or objects implementing the appropriate interfaces) using [] or {} silently returns NULL.


I guess that is what you would call undefined behaviour.

It makes sense, as a boolean can not have an offset.

error_reporting(E_ALL);
$d = TRUE; 
var_dump($d[0]);

This also produces NULL (if FALSE was coerced to an empty string, it would make sense that TRUE would be 1).

Plus what meze said :)


Quick look at the source code shows that this is expected behavior. But don't ask me why they did it this way...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜