Main Content

Compile but do not run PHP - syntax check only

Archive - Originally posted on "The Horse's Mouth" - 2010-02-22 10:33:05 - Graham Ellis

The -l option to PHP on the command line compiles but does not run the code in the file - so it's very useful for syntax checking your code.

Here's an example where I tested a file with a deliberate mistake:

-bash-3.2$ php -l wsse.php
Parse error: syntax error,
unexpected '{', expecting '(' in wsse.php on line 11
Errors parsing wsse.php
-bash-3.2$


And here's the results once I have corrected that:

-bash-3.2$ php -l wsse.php
No syntax errors detected in wsse.php
-bash-3.2$


The -l is the letter "l" - it stands for "lint". Lint was originally a program that was used in conjunction with C code on Unix systems, its purpose being to highlight things in the source code which weren't errors as such, but were highly questionable - to help you mop up little accidents before they happened, or to apply a bandage to a wound in the code before it became serious.

These days, the gcc compiler supports options such as -W to turn on warnings, and -ansi -pedantic to check for ANSI portability, and lint has been superseded.

Another useful code tidy program I've used in the past (for C) has been cb - the "Code Beautifier". That seems to have faded out like lint, but the indent program which (as the name suggests) indents your C blocks is still very much with us. And there are a lot of other options to indent too.

Tools for tidying up your PHP? ... have a look at the --with-tidy option when you build PHP; it's included in the distribution from PHP 5.