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.
  • Anonymous

    Syntax for find is incorrect.

  • Anonymous

    Syntax for find is incorrect.

  • mahler

    > Syntax for find i incorrect.

    The find syntax may differ depending on which version of find you have. On OSX 10.6.5 and RHEL 4 it works.
    Please check the find syntax, if it doesn’t work for you. The example above only looks in current directory, and find can look in a directory tree if you want that.

  • mahler

    > Syntax for find i incorrect.

    The find syntax may differ depending on which version of find you have. On OSX 10.6.5 and RHEL 4 it works.
    Please check the find syntax, if it doesn’t work for you. The example above only looks in current directory, and find can look in a directory tree if you want that.