0
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.

devnc
  • 70
  • 7
  • 3
    Are you sure the problem is on that line? – Zoli Szabó Dec 08 '20 at 11:07
  • Is it possible that `icon_path` is actually empty? Is might well _exist_ but if it's empty, you will be trying to access `$changedAttributes[""]` which of course will give you that error... – Stuart Dec 08 '20 at 11:07
  • I presume something is missing somewhere else in the code. – nice_dev Dec 08 '20 at 11:07
  • 1
    Please, always show the real error message ALL OF IT and the real code, enough of that so we are sure we are looking at the right line in the right file :) – RiggsFolly Dec 08 '20 at 11:15
  • @Stuart in that case they should get some sort of runtime error (and more likely saying undefined index), but not a parse error. – CBroe Dec 08 '20 at 11:29
  • Does this answer your question? [syntax error, unexpected T\_ENCAPSED\_AND\_WHITESPACE, expecting T\_STRING or T\_VARIABLE or T\_NUM\_STRING](https://stackoverflow.com/questions/8400018/syntax-error-unexpected-t-encapsed-and-whitespace-expecting-t-string-or-t-vari) – El_Vanja Dec 08 '20 at 11:31
  • 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. – devnc Dec 09 '20 at 00:57
  • Does this answer your question? [HEREDOC interfering with code indentation](https://stackoverflow.com/questions/2305869/heredoc-interfering-with-code-indentation) – user3783243 Dec 09 '20 at 02:02

1 Answers1

0

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;
    }
devnc
  • 70
  • 7
  • The script tags can be indented as you like. The only issue is with the identifier line `JSCRIPT;`. See the warning here, https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc This was changed in 7.3 so could be why you weren't seeing in 7.4. – user3783243 Dec 09 '20 at 02:01
  • @user3783243 thanks, properly indenting `JSCRIPT;` is enough. – devnc Dec 09 '20 at 02:27