About the Author

Chris Shiflett

Hi, I'm Chris, a web developer and a founding member of Analog. I live and work in Brooklyn, NY.


PHP Advent Calendar Day 17

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:

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. :-)

About This Post

PHP Advent Calendar Day 17 was posted on Mon, 17 Dec 2007 at 22:49:30 GMT.

6 Comments

1. Braden's GravatarBraden said:

To speed this trick up a bit, use subversion to find and lint modified files, as suggested by Maggie Nelson:

svn stat | grep -E ".php$" | awk "{print \"php -l \" \$2}" | sh

Mon, 17 Dec 2007 at 23:49:03 GMT Link


2. Amit Pansare's GravatarAmit Pansare said:

Thank you Ilia.

This is going into my pre-build script.

Hmm, Chris, you should do a post on what an ideal pre-build script should have.

Mon, 17 Dec 2007 at 23:52:05 GMT Link


3. Sergio V's GravatarSergio V said:

Good post. Just want to note you can do it all with a single command:

find /path/to/code -name '*.php' -exec php -l {} \;

Cheers!

Tue, 18 Dec 2007 at 03:03:35 GMT Link


4. Matt Southerden's GravatarMatt Southerden said:

IMHO, this kind of error should be caught by your unit and functional tests. Though maybe it could run as a safety net after the build (which would cause a build failure), prompting the examination of the test coverage?

Tue, 18 Dec 2007 at 11:30:24 GMT Link


5. Lode Claassen's GravatarLode Claassen said:

It works great!

However, it hangs on some files which I deliberately made non-php. I maybe should not make this .php files. But would there be another way of just excluding them from the search?

Mon, 24 Dec 2007 at 13:05:55 GMT Link


6. Andrew Johnstone's GravatarAndrew Johnstone said:

How about you do a lint check in a svn hook, thus avoiding syntax errors... PHP syntax check

Mon, 31 Dec 2007 at 08:35:04 GMT Link


Post A Comment

Personal Details and Comment

Style Guide

Line breaks are converted to paragraphs. Also use:

  • <a href="" title="">text</a>1
  • <em>text</em>
  • <blockquote><p>text</p></blockquote>
  • <code>2  <?php  if ($foo) {      $foo = TRUE;  }  ?></code>
  1. Note: <code> can be used inline (e.g. in paragraphs) or in a block as shown. Include whitespace and newlines in blocks.

Please enter Chris (my first name) below. This is a primitive spam prevention technique, and I apologize for the inconvenience.

Preview and Submit

Upcoming Talks

Kiwi Foo Camp

12 - 14 Feb 2010

At Mahurangi College, Warkworth, New Zealand.

Webstock

15 - 19 Feb 2010

At Wellington Town Hall, Wellington, New Zealand.

ConFoo

10 - 12 Mar 2010

At Hilton Montréal Bonaventure, Montréal, Canada.

South by Southwest

12 - 16 Mar 2010

At Austin Convention Center, Austin, Texas.

New Comments

Sujoy wrote:

Chris, this is the first time I'm visiting your blog! Your 2009 Highlights is really great! Fanta...

Posted in 2009 Highlights
Giovanni wrote:

Hi Chris! First of all, my persona thanks for all your article about PHP security! it's really u...

Posted in The Truth about Sessions
Chris Shiflett wrote:

Thanks, John. Friendly and trustworthy are high compliments. Much appreciated. :-) Sorry about...

Posted in 2009 Highlights
Eric B wrote:

Hi Chris, Thanks for this clean, concise article on this topic. You are a life saver! -E

Posted in Guru Speak: Storing Sessions in a Database
Radoslav Stankov wrote:

wow, I looks like 2009 wasn't very boring year. p.s. I didn't know you too are Arsenal fan.

Posted in 2009 Highlights

Browse Comments


Work and Books

Analog Essential PHP Security HTTP Developers Handbook