在 PHP7.2 中使用 HEREDOC 语法时出现“语法错误,意外的 '' (T_ENCAPSED_AND_WHITESPACE)”,但 PHP7.4 没问题

问题描述 投票:0回答:1
if (array_key_exists('icon_path', $changedAttributes)) {
    $iconFile = $changedAttributes["icon_path"];
}

为什么

$iconFile = $changedAttributes["icon_path"];
行在 php 7.2 中给出以下错误? 即使我将其更改为单引号
['icon_path']
也无法解决问题。

syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

但是在php 7.4中完全没有问题。

我用这个版本检查器检查了我的代码 https://www.piliapp.com/php-syntax-check/ 7.2 给了我这个错误,但 7.4 工作正常。

php runtime-error syntax-error heredoc
1个回答
0
投票

这是一个缩进问题。我的代码有以下 JavaScript 代码。

public function reloadPageOnEdit()
{
    return <<<JSCRIPT
        <script>
            function openWindowReload(link) {
                var href = link.href;
                
                document.location.reload(true);
                window.open(href,'_self');
            }
        </script>
        JSCRIPT;
}

我将其完全向左缩进。它解决了问题。即使在7.4中也没有问题,但在7.2中。 我很困惑,因为代码在其他地方失败了。

    public function reloadPageOnEdit()
    {
        return <<<JSCRIPT
<script>
    function openWindowReload(link) {
        var href = link.href;
        
        document.location.reload(true);
        window.open(href,'_self');
    }
</script>
JSCRIPT;
    }
© www.soinside.com 2019 - 2024. All rights reserved.