【问题标题】:use both shebang and strict type declaration in PHP 7在 PHP 7 中同时使用 shebang 和严格类型声明
【发布时间】:2016-08-04 10:12:44
【问题描述】:

我正在用 PHP7 编写一个命令行 PHP 脚本。

当我将 shebang (#!/usr/bin/php) 放在文件顶部时,如果我使用 declare(strict_types=1) 添加严格模式,我会收到以下错误:

PHP 致命错误:strict_types 声明必须是第 3 行 index.php 脚本中的第一条语句

我发现让严格类型工作的唯一方法是删除 shebang 行。

有没有办法同时使用 shebang 和 strict 类型或者它是一个 php 错误?

【问题讨论】:

  • 这应该对我有用。请提供完整的脚本以及如何调用它。也许您不小心插入了多余的换行符或类似内容?
  • 你是对的,shebang 和 php 开始标记之间有一个新行...我没有看到它,因为我使用插件自动添加 shebang 行。我觉得自己很蠢……
  • VTC 是简单的错字/不可重现。这就是为什么我们希望在问题中出现minimal reproducible example

标签: php command-line-interface php-7 shebang


【解决方案1】:

好的,这就是严格类型声明不起作用的原因:我在 shebang 和 php 开始标记之间有一个新行。

工作示例:

#!/usr/bin/php
<?php
declare(strict_types=1);

// your code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    相关资源
    最近更新 更多