"syntax error, unexpected '{' " 在关键字 'finally' 之后 运行 php cli

"syntax error, unexpected '{' " after keyword 'finally' when run php cli

我在尝试执行 PHP CLI 时遇到“PHP 解析错误:语法错误, 中出现意外的‘{’...”错误脚本。

错误信息:

PHP Parse error:  syntax error, unexpected '{' in ??/test.php on line 9

命令:

php -f ./test.php

代码:

<?php
 echo 'begin'.PHP_EOL;

 try {
    echo 'try!'.PHP_EOL;
 } catch (Exception $e) {
    echo 'exception'.PHP_EOL;
 } finally{
    echo 'finally'.PHP_EOL;
 }
 echo 'end';

PHP版本:PHP 5.4.16 (cli) (built: Aug 5 2016 07:50:38)

环境:RedHat 7.3

这是一个奇怪的情况,这个脚本在我的 Windows PC 上运行良好,但在 Redhat 机器上遇到了这个问题。

有人也遇到过这种情况吗?

您需要 PHP 5.5 或更高版本才能使用 finally 块。也许在 windows 机器上你有更高版本的 PHP?

最后关键字仅支持 PHP 5.5 或更高版本

PHP: Exceptions