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…

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone

Leave a Reply

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