我从Windows批处理文件中调用php lint,如下所示:
@echo off
for %%f in (*.php) do php -l %%f
当文件包含语法错误时,它只输出Errors parsing xxx.php
。有没有办法让它告诉我错误的本质是什么,它是什么线?也许是另一个开关?
答案 0 :(得分:36)
如果您收到“错误解析foo.php”消息而没有任何详细信息,则此代码将在您运行PHP lint时显示错误:
php -d display_errors=1 -l foo.php
谢谢查尔斯!
示例:
[somewhere]# php -l submit.php Errors parsing submit.php [somewhere]# php -d display_errors=1 -l submit.php Parse error: syntax error, unexpected T_VARIABLE in submit.php on line 226 Errors parsing submit.php
答案 1 :(得分:21)
我已经接受了查尔斯的回答,但我认为我应该添加一些细节,因为我不得不做一些额外的狩猎来找出该做什么。
问题是我没有看到stderr
输出,所以我开始将2>&1
添加到相关命令的末尾。这仍然没有帮助,所以我意识到问题是PHP根本没有输出stderr的东西。我转到PHP安装目录并查看php.ini,发现默认情况下display_errors
为Off
。将其更改为On
,现在可以使用了。
答案 2 :(得分:0)
您使用的是哪个版本的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的输出?