本文共 1017 字,大约阅读时间需要 3 分钟。
对于PHP4
…这转载的错误:
$foo = 'bar';
$foo[0] = 'bar';
对于PHP5
…这转载的错误:
$foo = 'bar';
if (is_array($foo['bar']))
echo 'bar-array';
if (is_array($foo['bar']['foo']))
echo 'bar-foo-array';
if (is_array($foo['bar']['foo']['bar']))
echo 'bar-foo-bar-array';
编辑,
so why doesn’t the error appear in the
first if condition even though it is a
string.
因为PHP是一个非常宽容的编程语言,我猜。我将用代码来说明我的想法:
$foo = 'bar';
// $foo is now equal to "bar"
$foo['bar'] = 'foo';
// $foo['bar'] doesn't exists - use first index instead (0)
// $foo['bar'] is equal to using $foo[0]
// $foo['bar'] points to a character so the string "foo" won't fit
// $foo['bar'] will instead be set to the first index
// of the string/array "foo", i.e 'f'
echo $foo['bar'];
// output will be "f"
echo $foo;
// output will be "far"
echo $foo['bar']['bar'];
// $foo['bar'][0] is equal calling to $foo['bar']['bar']
// $foo['bar'] points to a character
// characters can not be represented as an array,
// so we cannot reach anything at position 0 of a character
// --> fatal error