Syntax Error PHP Parse

PHP syntax errors are easy to make, and if unchecked can popup from the rarely used corners of a complicated application. If you short on time finding a debugger of some sort, don’t want to mess with installing software on the server, or just plain lazy, try this out at the command line:

find . -type f -name *.php -exec php -l {} ;

This would check all files with a “php” extension from the current directory (.) and down into the sub directories. I’m not claiming this is 100% accurate, but it sure is quick to type… or copy and paste.

If you wanted to fancy it up a bit, you might wrap it in PHP and do something like this (you could even run it against itself!):

<?php
 
$search_dir = "./";
$verbose = false;
 
if(count($argv) >= 2){
	if(is_dir($argv[1])) $search_dir = $argv[1];
}
if(count($argv) >= 3){
	if((bool)$argv[2] === true) $verbose = (bool)$argv[2];
}
 
echo("Testing $search_dirn");
 
exec('find '.$search_dir.' -type f -name *.php -exec php -l {} ;');
 
echo("Donen");

This allows you to execute from command line and pass in optional search directory and whether it will only show errors or everything. Here is an example for searching the sub directory “inc” and showing all files tested:

php check_syntax.php "inc/" true