Possible Duplicate:
Access array returned by a function in php可能的重复:访问php中函数返回的数组
The code:
代码:
$cnt = mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()"))[0]
Is giving the error:
给的错误:
Parse error: syntax error, unexpected '[' in index.php on line 117
解析错误:语法错误,意外的“[”在索引中。php在第117行
Same for:
同样的为:
$cnt = (mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()")))[0]
This code:
这段代码:
$cnt = mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()"));
$cnt = $cnt[0];
is working fine.
工作正常。
What's going on here?
这是怎么回事?
1 个解决方案
#1
4
It isn't just a problem with mysql_query
--rather, it's an idiosyncracy in the way that PHP <5.4 handles bracket notation. The following will fail, as well:
这不仅仅是mysql_query的问题——更确切地说,它是PHP <5.4处理括号表示法的一种特性。以下也将失败:
function get_array() {
return array('foo', 'bar');
}
echo get_array()[0];
But, as you observed, setting the result before attempting to retrieve an element works fine:
但是,正如您所观察到的,在尝试检索元素之前设置结果是可行的:
$arr = get_array();
echo $arr[0];
#1
4
It isn't just a problem with mysql_query
--rather, it's an idiosyncracy in the way that PHP <5.4 handles bracket notation. The following will fail, as well:
这不仅仅是mysql_query的问题——更确切地说,它是PHP <5.4处理括号表示法的一种特性。以下也将失败:
function get_array() {
return array('foo', 'bar');
}
echo get_array()[0];
But, as you observed, setting the result before attempting to retrieve an element works fine:
但是,正如您所观察到的,在尝试检索元素之前设置结果是可行的:
$arr = get_array();
echo $arr[0];