PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['

馋奶兔 提交于 2019-11-26 11:38:02

问题


Is it possible that this PHP code line

if ($this->greatestId()[\"num_rows\"] > 0)

works in PHP 5.5 and returns an error in 5.3??

PHP Parse error:  syntax error, unexpected \'[\' in /var/www/app/AppDAO.php on line 43

How can I change it to work under PHP 5.3?


回答1:


Array dereferencing became available in PHP 5.4 That's why this doesn't work in PHP 5.3. So you have an extra step where you need to get the array value from your function call and then you can use it:

$variable = $this->greatestId();
if ($variable["num_rows"] > 0){
      // do stuff
}



回答2:


You cant use like this if ($this->greatestId()["num_rows"] > 0) in PHP 5.3 ver use below code.

$var = $this->greatestId();
if ($var["num_rows"] > 0){
  // your code
}



回答3:


As mentioned in the PHP 5.4 notes:

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

It's not possible to do that in PHP 5.3, you need to use a variable.



来源:https://stackoverflow.com/questions/23565971/php-5-3-10-vs-php-5-5-3-syntax-error-unexpected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!