And*_*rew 2 php multidimensional-array undefined-index
我已经打开了所有错误报告,以清理一些未定义的索引,只是为了使我制作的应用程序更加简洁。我注意到一种奇怪的行为:
假设我有以下数组: $a = array('test' => false, 'foo' => 'bar')
如果这样if ($a['nothere'])
,我会适当的通知Undefined index: nothere
。
但是,如果这样做if ($a['test']['nothere'])
,我不会收到通知。完全没有 尽管nothere
绝对不是的索引$a['test']
。
现在,如果我这样做了$a['test'] = array('baz' => 'poof')
,那么运行if ($a['test']['nothere'])
确实会发出通知。
未定义索引检查是否不检查空数组中的索引?这是在PHP 5.2.8上。
这是因为棘手的类型杂耍。$ a ['test']被强制转换为[empty]字符串,然后'nowhere'被强制转换为0,然后PHP尝试在空字符串中查找0的符号。它成为子字符串操作,而不是变量查找,因此,不会引发任何错误。
e,一个人说的也一样:http : //php.net/manual/en/language.types.type-juggling.php
根据我的经验,一个很好奇的例子:
通过超链接被请求index.php?sname=p_edit&page=0
,代码
if (isset($_GET['sname'])) { $page['current'] = $_GET['sname'].'.php'; };
echo $page['current'];
Run Code Online (Sandbox Code Playgroud)
只产生一个字母 "p"
我计算了6个步骤和2种类型的转换,最终得到了这样的结果。
(提示:问这个问题的可怜的家伙已经在全球注册了)