由于速度的提高,我只是测试了PHP 7,现在我有以下代码导致不可预测的结果:
<?php
$_SESSION ['test'] [ 't****' ] = 'Hello';
var_dump($_SESSION);
echo "\n" . $_SESSION [ 'test' ] [ 't****' ];
$var = "test2";
for ($i = 4 ; $i > 0 ; $i--){
$var [$i] = "*";
echo "\n" . $_SESSION['test'][$var];
}
?>
在这个例子中,我期望3次未定义的索引错误。应找到最后一个索引并返回Hello
。这适用于PHP 5.6,但不适用于PHP 7.
在PHP 7中,我得到以下结果:
array(1) {
["test"]=>
array(1) {
["t****"]=>
string(5) "Hello"
}
}
Hello
Notice: Undefined index: test* in /in/YANSb on line 12
Notice: Undefined index: tes** in /in/YANSb on line 12
Notice: Undefined index: te*** in /in/YANSb on line 12
Notice: Undefined index: t**** in /in/YANSb on line 12
在PHP 5.6中,我得到了预期的结果:
array(1) {
["test"]=>
array(1) {
["t****"]=>
string(5) "Hello"
}
}
Hello
Notice: Undefined index: test* in /in/YANSb on line 12
Notice: Undefined index: tes** in /in/YANSb on line 12
Notice: Undefined index: te*** in /in/YANSb on line 12
Hello
我able重现错误。
奇怪的是$var === "t****"
在true
时返回$i == 1
,但$_SESSION ['test'] [$var]
将返回索引未定义错误,尽管$_SESSION ['test'] ['t****']
将返回Hello
}。那么,我今天是在寻找一个虫子吗?