空变量的键等于空而不是错误 [英] Key of null variable equals null not error

查看:63
本文介绍了空变量的键等于空而不是错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我们有一些变量,我们认为这将是一个数组,但它恰好为空.

We've got variable that for some reason we think would be an array, but it happens to be null.

$var = null

我们尝试从此变量中获取一个值.

We try to get a value from this variable.

$value = $var['key']

这不会引发错误,我的直觉是会的.相反,发生的是$ value现在也为空.上面的行没有抛出错误是有特殊的原因吗?

This doesn't throw an error, my intuition is that it would though. What instead happens is that $value is now also null. Is there a particular reason that the above line doesn't throw an error?

推荐答案

几乎重复":

There is "almost duplicate": Why does accessing array index on boolean value does not raise any kind of error?

其中的代码如下:

$var = false;
$value = $var['key'];

答案是-它是仅文档

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

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

所以在这个字符串中(我说的是您的情况,$ var = null,但使用布尔值将是相同的解释,只需将NULL替换为布尔值即可)

So in this string (I am talking about your case, $var = null, but with boolean would be the same explanation, just replace NULL to boolean)

$var['key']

$ var是类型NULL 的变量,并访问使用[]的NULL类型(数组或对象的其他类型)的变量会默默地返回NULL.

$var is the variable of type NULL, and accessing variable of type NULL (other type that array or object) using [] silently returns NULL.

这篇关于空变量的键等于空而不是错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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