【问题标题】:PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected '['PHP 5.3.10 vs PHP 5.5.3 语法错误意外'['
【发布时间】:2014-06-27 06:34:20
【问题描述】:

这个PHP代码行有没有可能

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

在 PHP 5.5 中工作并在 5.3 中返回错误??

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

如何将其更改为在 PHP 5.3 下工作?

【问题讨论】:

    标签: php arrays php-5.3 dereference php-5.5


    【解决方案1】:

    Array dereferencing 在 PHP 5.4 中可用这就是为什么这在 PHP 5.3 中不起作用的原因。所以你有一个额外的步骤,你需要从你的函数调用中获取数组值,然后你可以使用它:

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

    【讨论】:

    • 找到了链接:php.net/manual/en/migration54.new-features.php 在他们说的新功能中:添加了函数数组解引用,例如foo()[0].
    • 我昨天遇到了这个错误,在我的本地环境中我有 php5.5 并在测试 5.3 :/感谢您的回答。
    【解决方案2】:

    在 PHP 5.3 版本中你不能像这样使用 if ($this->greatestId()["num_rows"] > 0) 使用下面的代码。

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

    【讨论】:

      【解决方案3】:

      如 PHP 5.4 注释中所述:

      从 PHP 5.4 开始,可以取消对数组结果的引用 直接调用函数或方法。以前只能使用 临时变量。

      在 PHP 5.3 中无法做到这一点,您需要使用变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-12
        • 1970-01-01
        • 2011-02-09
        • 1970-01-01
        • 2012-05-16
        • 2015-05-28
        相关资源
        最近更新 更多