【问题标题】:PHP 7.1 not recognizing the PHP5.6+ syntax `use function`PHP 7.1 无法识别 PHP5.6+ 语法“使用函数”
【发布时间】:2018-07-23 05:31:20
【问题描述】:

我有一个在 Apache 和 PHP 7.1 上运行的 PHP 网站。 该应用程序在我的 PC 上运行良好,当我部署到服务器时出现以下错误:

PHP Parse error: syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) or \\\\ (T_NS_SEPARATOR)

当我查看错误发生的位置时,据我了解,这是一种 PHP5.6+ 语法:

use function ...;

当我运行 php --versionwhich php 时,它显示它是 PHP 7.1。 是否有配置 php 以允许高于特定版本的语法的设置?

编辑: phpinfo() 报告 PHP 5.5.9 我想我需要的是关于如何设置 Apache 以使用默认 PHP 版本的帮助,如果我的服务器是 7.1。

【问题讨论】:

  • 您确定您的 Apache 使用保存 php 版本作为您的 Web 服务器的 cli 吗?将phpinfo();exit; 放入代码中并向我们展示结果。
  • @LawrenceCherone use function 在 PHP 中是有效的 use statement
  • phpinfo 正在报告 PHP 版本 5.5.9,我想我需要将 apache 设置为使用默认 PHP,在这种情况下为 7.1。我将编辑问题
  • @LawrenceCherone 如果我们不知道导入的函数,那你怎么肯定说因为function而抱怨
  • @LawrenceCherone,正如我之前所说,相同的代码正在我的 PC 上运行,因为我的 PC 正确运行 PHP 7.1。所以问题都与PHP版本有关,而不是代码。

标签: php apache


【解决方案1】:

答案是禁用模块 PHP 5 并为 apache 启用 7.1:

sudo a2dismod php5
sudo a2enmod php7.1
sudo service apache2 restart

【讨论】:

    【解决方案2】:
    use keywork will support all version of php. i think your function declaration error. 
    you can use below example and correct your function
    **use** can only be used with anonymous functions. Using it with named functions 
     gives a syntax error.
    
     <?
    function counter($start) {
      $value = $start;
    
     return function() use (&$value) {
       return $value++;
     };
    

    }

     $counter = counter(6);
    
    echo $counter() . "\n";
    echo $counter() . "\n";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 2017-06-04
      • 1970-01-01
      相关资源
      最近更新 更多