phpruntime-errorsyntax-errorheredoc

"syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE)" while using HEREDOC syntax in PHP7.2, but PHP7.4 is fine


if (array_key_exists('icon_path', $changedAttributes)) {
    $iconFile = $changedAttributes["icon_path"];
}

Why does $iconFile = $changedAttributes["icon_path"]; line gives me below error in php 7.2? Even though I change it to single quotations ['icon_path'] doesn't solve the problem.

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

But in php 7.4 there's no problem at all.

I checked my code with this version checker https://www.piliapp.com/php-syntax-check/ 7.2 gives me this error but 7.4 works fine.


Solution

  • It was an indent issue. My code has below JavaScript code.

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

    and I indented it fully left. It solved the problem. Even there's no issue in 7.4 but 7.2. I got confused because the code failed on somewhere else.

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