PHP 数组语法解析错误左方括号“[";
问题描述:
我有一个返回数组的函数.我有另一个函数只返回第一行,但由于某种原因,它让我使用一个中间变量,即失败:
I have a function that returns an array. I have another function that just returns the first row, but for some reason, it makes me use an intermediate variable, i.e. this fails:
function f1(/*some args*/) {
return /*an array*/;
}
function f2(/*some args*/) {
return f1(/*some args*/)[0];
}
...与:
解析错误:语法错误,第 10 行 util.php 中出现意外的["
Parse error: syntax error, unexpected '[' in util.php on line 10
但是,这是有效的:
function f1(/*some args*/) {
return /*an array*/;
}
function f2(/*some args*/) {
$temp = f1(/*some args*/);
return $temp[0];
}
我无法在网上找到任何相关内容(我的搜索一直被带有?"、{"、<"等的人弄糊涂).
I wasn't able to find anything pertinent online (my searches kept getting confused by people with "?", "{", "<", etc.).
我是自学 PHP - 有什么原因我不能直接做到这一点,但我错过了吗?
I'm self-taught in PHP - is there some reason why I can't do this directly that I've missed?
答
不能使用函数数组解引用
You can't use function array dereferencing
return f1(/*some args*/)[0];
直到 PHP 5.4.0 及更高版本.