首页 > 解决方案 > 当表达式作为闭包内数字数组的索引传递时,为什么PHP会抛出错误

问题描述

我有一个在 Ubuntu 上运行 Apache2、PHP7.4 的开发环境和一个在 Ubuntu 上运行 Apache2、PHP7.2 的生产服务器。以下代码在我的开发环境中运行良好,但在生产环境中失败并出现错误[2020-07-25 13:00:16] production.ERROR: syntax error, unexpected '[', expecting ']' {"exception":"[object] (ParseError(code: 0): syntax error, unexpected '[', expecting ']' at /path/to/file.php:133)。这是失败的行:$month = $months[$months[$i - 1]];

function myFunction(){
    $months = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
    return (function()use($months){
            $ret = [];
            $i = 1;
            while($i < 13)
            {
                $month = $months[$i - 1];
                // some more code that updates $ret ...
                $i++;
            }
            return $ret;
        })();
}

我会感谢任何关于为什么失败的想法。

标签: phparrays

解决方案


推荐阅读