Automatically checking for php parse errors

27 Jul 2006

I’ve been running into a problem on one of my multi-developer sites where we’ve been unable to break the habit of occasionally allowing parse errors to show up on one of our php sites. We mean well, we try to avoid it, but sometimes a quick fix is necessary and we don’t always have time to do testing (it should be noted that we don’t have a testing harness of any kind for this site).

Just last weekend I was horrified to find that I’d left a parse error in a small, seldom-visited part of our site for several days. No users encountered it but it prevented some automated tasks from working as they’re supposed to.

It’s become clear that we need more than just to try harder - we need technology to watch our backs. Do accomplish this I’ve developed a bash script that takes directories as arguments and has php parse the contents of every php script in that folder’s structure.

The Code:


    check_dir() {
      for file in $*/*.php
        if [ -d $file ]; then
          echo "going into $file"
          check_dir $file
          if [ "`php -l $file`" = "No syntax errors detected in $file" ]; then
            echo "valid in $file"
            echo "$file invalid"

    for dir in $*
      check_dir $dir

It’s very simple - but it’s all that’s required to get started. From here I’m going to replace the echo’ed output with tasks. Specifically, a php file will be run when files are found to be invalid and both an email and an SMS will be sent to the developer’s phones.

We’re setting this script to run every 5 minutes, hopefully no weekends will pass from now on without being error- (at least parse error-) free.

Please if you found this post helpful or have questions.