为什么在布尔值访问数组索引不会引发任何类型的错误? [英] Why does accessing array index on boolean value does not raise any kind of error?

查看:169
本文介绍了为什么在布尔值访问数组索引不会引发任何类型的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问者皆阵列这是不存在这个数组中,PHP将提高未定义指数通知错误。当我尝试做同样的字符串,非法的字符串偏移量出现警告。这是预期的行为,我知道如何对付它。

When I try to access an array by key which is not exists in this array, php will raise "undefined index" notice error. When I try to do the same on strings, "Illegal string offset " warning is raised. This is an expected behavior and I know how to deal with it.

但是,当我尝试这样做的布尔或整数什么也不会发生:

But when I tried this on boolean or integer values nothing happens:

ini_set('display_errors', 1);
error_reporting(E_ALL);

$var = false;
var_dump($var['test']);

我希望看到一些错误消息,但是$ var ['测试'] 只是默默地设置为NULL。

I expect to see some error messages, but $var['test'] just silently sets to NULL.

那么,为什么PHP允许通过一个数组的键访问,而你正在做的事情不对任何迹象显示布尔值?
孔,通过数组键访问布尔值这句话听起来非常奇怪的我,但你可以在PHP做到这一点。

So why does php permit to access boolean value through an array key without any indication that you are doing something wrong? The hole "access boolean value through an array key" phrase sounds terribly wierd to me, but you can do it in php.

推荐答案

这是可悲的,但它记录的行为。

It's sad, but it's documented behaviour.

<一个href=\"http://php.net/manual/en/language.types.string.php\">http://php.net/manual/en/language.types.string.php

请注意:

使用[]或{}默默地返回NULL访问其他类型的变量(不包括实施适当的接口数组或对象)。

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

这篇关于为什么在布尔值访问数组索引不会引发任何类型的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆