PHP 编译错误:“不能在数组中使用空数组元素" [英] PHP Compile Error: "Cannot use empty array elements in arrays"

查看:45
本文介绍了PHP 编译错误:“不能在数组中使用空数组元素"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 bepsvpt/secure-headers 包的 Laravel 5 项目使用以下配置文件:

I have a Laravel 5 project that is using the bepsvpt/secure-headers package with the following config file:

<?php

return [
    'x-content-type-options' => 'nosniff',
    'x-download-options' => 'noopen',
    'x-frame-options' => 'sameorigin',
    'x-permitted-cross-domain-policies' => 'none',
    'x-xss-protection' => '1; mode=block',

    /*
     * Referrer-Policy
     *
     * Reference: https://w3c.github.io/webappsec-referrer-policy
     *
     * Available Value: 'no-referrer', 'no-referrer-when-downgrade', 'origin', 'origin-when-cross-origin',
     *                  'same-origin', 'strict-origin', 'strict-origin-when-cross-origin', 'unsafe-url'
     */

    'referrer-policy' => 'strict-origin-when-cross-origin',

    'hsts' => [
        'enable' => env('SECURITY_HEADER_HSTS_ENABLE', false),
        'max-age' => 15552000,
        'include-sub-domains' => false,
    ],

    /*
     * Content Security Policy
     *
     * Reference: https://developer.mozilla.org/en-US/docs/Web/Security/CSP
     *
     * csp will be ignored if custom-csp is not null.
     *
     * Note: custom-csp does not support report-only.
     */

    'custom-csp' => env('SECURITY_HEADER_CUSTOM_CSP', null),

    'csp' => [
        'report-only' => false,

        'report-uri' => env('CONTENT_SECURITY_POLICY_REPORT_URI', false),,

        'upgrade-insecure-requests' => false,

        'base-uri' => [
            //
        ],

        'default-src' => [
            //
        ],

        'child-src' => [
            //
        ],

        'script-src' => [
            'allow' => [
                //
            ],

            'hashes' => [
                // ['sha256' => 'hash-value'],
            ],

            'nonces' => [
                //
            ],

            'self' => false,

            'unsafe-inline' => false,

            'unsafe-eval' => false,
        ],

        'style-src' => [
            'allow' => [
                //
            ],

            'self' => false,

            'unsafe-inline' => false,
        ],

        'img-src' => [
            'allow' => [
                //
            ],

            'types' => [
                //
            ],

            'self' => false,

            'data' => false,
        ],

        /*
         * The following directives are all use 'allow' and 'self' flag.
         *
         * Note: default value of 'self' flag is false.
         */

        'font-src' => [
            //
        ],

        'connect-src' => [
            //
        ],

        'form-action' => [
            //
        ],

        'frame-ancestors' => [
            //
        ],

        'media-src' => [
            //
        ],

        'object-src' => [
            //
        ],

        /*
         * plugin-types only support 'allow'.
         */

        'plugin-types' => [
            //
        ],
    ],
];

当我尝试运行应用程序(网络请求或 php artisan)时,出现以下错误:

When I try to run the application (web request or php artisan), I get the following error:

PHP Fatal error:  Cannot use empty array elements in arrays in C:\Web\myapp\config\secure-headers.php on line 4

当然,文件的第 4 行看起来完全没问题!

Of course, line 4 of the file looks totally fine!

这里有什么问题?

推荐答案

这个错误,我可以在网上找到的任何地方都没有记录,来自于连续两个逗号,数组中它们之间没有任何东西.

This error, which is not documented anywhere I can find online, comes from having two commas in a row with nothing between them inside the array.

在我的例子中,这实际上出现在文件的第 42 行,而不是错误消息指示的第 4 行,这听起来像是编译器中的一个错误,它标识了数组中的第一项而不是实际位置空数组元素".

In my case, this actually appeared on line 42 of the file, not line 4 as indicated by the error message, which sounds like a bug in the compiler which identifies the first item in the array instead of the actual location of the "empty array element".

注意:在 PHP 7.2.15+、7.3.2+ 和 7.4.0+ 中,错误消息已更改为报告前一个有效元素的行号而不是数组开头的行号.虽然这可能仍然偏离一行或多行,但它通常与问题足够接近,因此更容易找到.

NOTE: In PHP 7.2.15+, 7.3.2+, and 7.4.0+, the error message has been changed to report the line number of the previous valid element instead of the line number of the beginning of the array. While this may still be off by one or more lines, it is usually close enough to the problem to make it much easier to find.

这篇关于PHP 编译错误:“不能在数组中使用空数组元素"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆