views:

33

answers:

2

I'm calling php lint from a Windows batch file, like so:

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

When a file contains a syntax error, it only outputs Errors parsing xxx.php. Is there any way to get it to tell me what the nature of the error is, and what line it's on? Maybe another switch?

A: 

What version of PHP are you using? As of 5.3, line numbers are included in lint output:

[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 ~]$

The error appears twice because it's going to both stdout and stderr. It's been a long time since I've worked with batch files on Windows, maybe the version you're using only emits the error on stderr, and the batch file is discarding the output of stderr?

Charles
This helped me find out what the problem was, so I've accepted it. See my answer for more details.
Hammerite
+1  A: 

I've accepted Charles's answer, but thought I should add a couple of details, as I had to do some extra hunting to find out what to do.

The problem was that I wasn't seeing the stderr output, so I started by adding 2>&1 to the end of the relevant commands. This still didn't help, so I realised that the problem was that PHP wasn't outputting stderr stuff at all. I went to the PHP install directory and looked in php.ini and found that by default, display_errors is Off. Changed it to On and it now works.

Hammerite
Oh my, that was unexpected. You'd think that *asking PHP to check the syntax of a file* would, by default, report syntax errors! Keep in mind that you can manipulate php.ini arguments from the command line using the `-d` option: `php -d display_errors=1 -l foo.php`
Charles