Home > Development, Documentation, English, php > Detect syntax errors in multiple PHP files with xargs

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…

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: