解决PHP报错:PHP Parse error: syntax error, unexpected ‘?’

     0评论

本文分析并解决【PHP Parse error: syntax error, unexpected '?'】错误。

开发机上安装的PHP版本是7.0,但运行一个项目时报错:PHP Parse error: syntax error, unexpected ‘?’,出错文件在vendor的一个依赖项目上。虽然觉得不太可能,还是去改了一下,是?? null的语法,这个PHP 7.0是支持的,不明白为什么报错。果然,修改后又有另外一个依赖项目的文件报此错误,显然,这是碰上运行环境的问题了,于是将修改过的文件恢复原样,并Google了一下。

在StackOverflow上有问题描述这个错误,题主说:

But as i know, null coalescing operator( ?? ) is supported from PHP 7.0

答主告诉他:

If I had to guess, I’d say you installed the PPA 7.1.8 as CLI only (php7-cli). You’re getting your version info from that, but your libapache2-mod-php package is still 14.04 main which is 5.6. Check your phpinfo in your browser to confirm the version. You might also consider migrating to Ubuntu 16.04 to get PHP 7.0 in main.

也就是说,你在cli环境运行的是PHP7,但apache上跑的是PHP5。

于是我用phpinfo()看来一下,果然是这样。

vim i.php # 写入了 mv i.php public/ # 将文件放到虚拟主机的根目录后访问,查看PHP信息

问题明确后,安装apache的php模块,停用php5的模块并启用新的:

apt-get install libapache2-mod-php7.0 ls /etc/apache2/mods-available/ # 确认了下安装后的模块名称,是php7.0 a2dismod php5.6 a2enmod php7.0 service apache2 restart # 重启服务

至此,问题解决。

参考资料

-- EOF --

本文最后修改于6年前 (2018-06-19)

差评不太行一般挺好非常不错 (No Ratings Yet)
读取中...
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址