【问题标题】:Why does this php array function gives me an error为什么这个php数组函数给我一个错误
【发布时间】:2021-03-19 16:52:45
【问题描述】:
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 工作正常。

【问题讨论】:

  • 你确定问题出在那一行吗?
  • 有没有可能icon_path 实际上是空的?很可能存在,但如果它是空的,你将尝试访问$changedAttributes[""],这当然会给你这个错误......
  • 我认为代码中的其他地方缺少某些东西。
  • 请始终显示真正的错误消息 ALL OF IT 和真正的代码,足够了,所以我们确定我们正在查看正确文件中的正确行 :)
  • @Stuart 在这种情况下,他们应该得到某种运行时错误(更有可能是未定义的索引),但不是解析错误。

标签: php syntax-error runtime-error


【解决方案1】:

这是一个缩进问题。我的代码有以下 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;
    }

【讨论】:

  • 脚本标签可以任意缩进。唯一的问题是标识符行JSCRIPT;。请参阅此处的警告,php.net/manual/en/… 这在 7.3 中已更改,因此可能是您在 7.4 中没有看到的原因。
  • @user3783243 谢谢,正确缩进JSCRIPT; 就足够了。
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 2011-09-13
  • 2021-08-09
  • 2013-11-14
相关资源
最近更新 更多