PHP Advent Calendar Day 17

17 Dec 2007

Today's entry is provided by Ilia Alshanetsky.

Ilia Alshanetsky

Name
Ilia Alshanetsky
Blog
ilia.ws
Biography
Ilia Alshanetsky is an active member of the PHP development team and is the current release manager for PHP 5.2. Ilia is also the principal developer of FUDforum, an open source bulletin board, and he contributes to several other projects.
Location
Toronto, Canada

I often work with very large projects that contain hundreds or even thousands of files, and I have observed a common and rather embarrassing mistake: parse errors. Developers forget to check the syntax of their code, and as a result, the application starts displaying E_PARSE errors to users. Fortunately, there is an easy way to quickly check your code for silly parse errors with the following command:

  1. find /path/to/code -name \*.php | xargs -n1 php -l

This command searches through the /path/to/code directory for all files with a .php extension (adjust as necessary to match your own naming conventions; add another extension such as .inc by appending -o -name \*.inc to the find command) and passes them one at a time to PHP's CLI binary with -l to indicate lint mode. In this mode, the file is parsed but not executed, and any existing parse errors will be identified. The scripts stops execution if a parse error is encountered.

You can typically check few hundred files for parse errors within a few seconds. You now have no excuse for allowing parse errors to escape into the wild. :-)