PHP-“文件”;未定义索引“;请注意,在引用(&;)关联数组的未实例化元素时未引发此问题
我从以下内容查看此代码: 仅感谢对PHP-“文件”;未定义索引“;请注意,在引用(&;)关联数组的未实例化元素时未引发此问题,php,arrays,indexing,reference,undefined,Php,Arrays,Indexing,Reference,Undefined,我从以下内容查看此代码: 仅感谢对$temp=&$temp[$key]的改进,其结果是: $res = [ "item1" => [ "item2" => [ "itemx" => & null ]]] 我不明白为什么&temp[$key]实例化关联[$key=>null]而$temp[$key]没有 我做了一些调试,对于第一个$key(项目1): $a=$temp[$key]
$temp=&$temp[$key]
的改进,其结果是:
$res = [
"item1" => [
"item2" => [
"itemx" => & null
]]]
我不明白为什么&temp[$key]
实例化关联[$key=>null]
而$temp[$key]
没有
我做了一些调试,对于第一个$key
(项目1):
- 给出“未定义索引”通知
- 转储
返回空值$a
- 转储
返回空值$temp
- 不发出“未定义索引”通知
- 转储
返回空值$a
- 转储
返回null$temp[$key]
- 转储
返回:$temp
[“item1”=>&null]
$temp=&$temp[$key]
也实例化$temp[$key],或者在其前面加上以下等效项:
$temp = &temp[$key]; <=> $temp[$key] = null; $temp = &temp[$key];
$temp=&temp[$key]$临时[$key]=null$temp=&temp[$key];
我想了解php文档中是否解释了这一点(我搜索过,但没有找到任何东西),或者我是否遗漏了一些明显的东西
谢谢文档中对此进行了解释。创建引用时会创建未定义的变量: 注意:
如果通过引用指定、传递或返回未定义的变量,将创建该变量 示例#1使用带有未定义变量的引用
<?php
function foo(&$var) { }
foo($a); // $a is "created" and assigned to null
$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)
$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
?>
这正是我要找的!非常感谢。
<?php
function foo(&$var) { }
foo($a); // $a is "created" and assigned to null
$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)
$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
?>