gpt4 book ai didi

php - 为什么第一次调用没有E_NOTICE错误?

转载 作者:可可西里 更新时间:2023-11-01 12:40:01 25 4
gpt4 key购买 nike

我有以下代码片段:

 error_reporting(E_ALL | E_STRICT);

function &getVal() {
$data = [];

return $data['hey'];
//return $whatever;
}

function getVal2() {
$data = [];

return $data['hey'];
}

var_dump(getVal()); // No E_NOTICE error is issued - why?
var_dump(getVal2()); // E_NOTICE error is issued.

问题是:为什么第一次调用没有E_NOTICE错误?解释很可能是创建变量 $data['hey'] 以返回引用。但是,当 $data['hey'](或 $whatever...)时不发出 E_NOTICE 错误似乎仍然是错误的未定义。

最佳答案

这是预期的行为

http://www.php.net/manual/en/language.references.whatdo.php#language.references.whatdo.assign

If you assign, pass, or return an undefined variable by reference, it will get created.

和一些相关的“错误”:

https://bugs.php.net/bug.php?id=30350

Ok, it appears that the element is created because we are attempting to return a reference to something that does not exist.

https://bugs.php.net/bug.php?id=27627

When you try to access a non-existant array element you effectively create it, hence the NULL entries in the array.

关于php - 为什么第一次调用没有E_NOTICE错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20646486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com