【问题标题】:Why doesn't lint tell me the line number and nature of the parse error?为什么 lint 不告诉我解析错误的行号和性质?
【发布时间】:2011-03-21 08:58:54
【问题描述】:

我正在从 Windows 批处理文件中调用 php lint,如下所示:

@echo off
for %%f in (*.php) do php -l %%f

当文件包含语法错误时,它只输出Errors parsing xxx.php。有没有办法让它告诉我错误的性质是什么,它在哪一行?也许另一个开关?

【问题讨论】:

    标签: php lint


    【解决方案1】:

    如果您收到“Errors parsing foo.php”消息而没有任何详细信息,则此参数会在您运行 PHP lint 时显示错误:

    php -d display_errors=1 -l foo.php

    例子:

    [某处]# php -l submit.php
    
    解析 submit.php 时出错
    
    [某处]# php -d display_errors=1 -l submit.php
    
    解析错误:语法错误,第 226 行 submit.php 中的意外 T_VARIABLE
    解析 submit.php 时出错

    【讨论】:

      【解决方案2】:

      我已经接受了 Charles 的回答,但我认为我应该添加一些细节,因为我必须进行一些额外的搜索才能找出该怎么做。

      问题是我没有看到stderr 输出,所以我首先将2>&1 添加到相关命令的末尾。这仍然没有帮助,所以我意识到问题在于 PHP 根本没有输出 stderr 的东西。我到PHP安装目录查看php.ini,发现display_errors默认为Off。将其更改为On,现在可以使用了。

      【讨论】:

      • 天哪,这真是出乎意料。您会认为要求PHP 检查文件的语法 默认情况下会报告语法错误!请记住,您可以使用-d 选项从命令行操作 php.ini 参数:php -d display_errors=1 -l foo.php
      • 感谢您的提示! (请接受这个答案,而不是上面不正确的答案 ;-)
      【解决方案3】:

      您使用的是什么版本的 PHP?从 5.3 开始,行号包含在 lint 输出中:

      [charles@server ~]$ cat syntax_error.php
      <?php
      echo "This line is legal\n";
      echo I'm a syntax error!\n;
      echo "This line never gets reached.\n"
      
      [charles@server ~]$ php -l syntax_error.php
      PHP Parse error:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ',' or ';' in syntax_error.php on line 3
      
      Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ',' or ';' in syntax_error.php on line 3
      Errors parsing syntax_error.php
      [charles@server ~]$
      

      错误出现两次,因为它会同时发送到 stdout 和 stderr。自从我在 Windows 上使用批处理文件已经很长时间了,也许您使用的版本仅在 stderr 上发出错误,而批处理文件正在丢弃 stderr 的输出? p>

      【讨论】:

      • 这帮助我找出了问题所在,所以我接受了它。详情请参阅我的回答。
      • 您能否接受您在下面发布的答案,因为它实际上解释了问题?
      • @Hammerite 请切换 Accepted 标志 - 我会删除此答案以使其更明显,但您不能自行删除已接受的答案。
      • 感谢查尔斯的好意! :)
      猜你喜欢
      • 2012-04-29
      • 2021-06-28
      • 2013-07-25
      • 1970-01-01
      • 2019-02-20
      • 2017-02-20
      • 1970-01-01
      • 2020-02-16
      • 2010-11-22
      相关资源
      最近更新 更多