Syntax checking PHP on the commandline

I’m sure most people only thing of PHP as a Weblanguage due to be called through a browser. It has however since version 4.3.0 also been possible to use PHP on the commandline – as you do with Perl, Shell scripts and likewise. If you’re using Linux (or an other Unix-like operating system – including Mac OSX) you probably have a few small programs available which can make it a breeze to check if the syntax in all you PHP scripts is correct.

Here’s how. On the commandline type:

find *.php |xargs -I {} php -l {}

… and here’s an explaination of what it does:

  • “find *.php” findes all files with a dot php ending.
  • the pipe-character “|” sends the result to a command called xargs.
  • “xargs -I {} php -l {}” takes the lines one by one password from the find and call “php -l <line input>”.
  • “php -l <line input>” (where line input would be the php-files found in bullit one) runs php with the “lint” parameter which does the syntax checking.