Detect syntax errors in multiple PHP files with xargs

With the Command Line Interpreter for PHP (most of the time called php-cli or php5-cli), you can check if one single file contains syntax errors by simply calling

php5 -l file.php

The problem with that is, if you have many files to check, on the command line you might easily spend your time typing filenames. Luckily, the xargs command is meant to treat multiple files at ones and launch a simple command on all these files.

So the magical formula, if you have many files ending with .inc.php in your current directory, would be:

find . -name "*.inc.php" -type f -print0 | xargs -0 -I {} php5 -l {}

The find command at the beginning (ending with “-print0” where the last letter is a zero, not an ooh), just lists the files in one single line, separated by spaces.

The xargs command then takes this list, and for every file (removing problems of white-spaces in filenames with -0 and using -I to define the list marker in the following command), executes the php5 -l command on the list marker.

Man, I love it when a plan comes together…


Related Posts

Benchmark: dirname(__FILE__) vs __DIR__ in PHP

dirname(__FILE__) and __DIR__ give exactly the same result in PHP, although one (__DIR__)...

PHPEclipse + Xdebug + Firefox

There’s a short but great article about setting up PHPEclipse with Xdebug here:...

Leave a Comment

Your email address will not be published. Required fields are marked *

Add Comment *

Name *

Email *

Website *

Quick Contact Form