Check a PHP File for Syntax Errors from the Command Line

I sometimes find myself having to create PHP scripts from scratch using Nano on a command line only interface on a Ubuntu Linux server. Obviously this kind of sucks if you are used to using a proper error-checking and code hinting full IDE like Aptana to help speed up your scripting, but hey, you can’t always have things your way.

Anyway, the point of this quickfire post is to remind you of PHP’s built in syntax checker that comes bundled in with the main PHP executable as a switch…

…namely the -l (lowercase ‘L’) switch.

Simply point the php -l command at the PHP file you wish to check for syntax errors (via a command prompt if you’re using Windows or a terminal if you’re partaking in the Linux experience) and the application will spit out a result for you, hopefully the much appreciated “No syntax errors detected in scriptname” success string.

A nice and simple way of ensuring that your PHP script at least passes all the compiler tests in terms of syntax and will actually run when executed. :)

Related Link: http://php.net/manual/en/features.commandline.php

About Craig Lotter

Craig Lotter is a 29-ish year old software and web developer by trade (currently working for Touchwork), who also just happens to never have been able to shake off that pesky inner child within. Call him a fanboy, geek, nerd or whatever you want, just so long as you enjoy what he writes. His main personal site can be found at http://www.craiglotter.co.za and his webcomic, House of C can be found at http://www.houseofc.codeunit.co.za/
This entry was posted in Tutorials and tagged , , , , , . Bookmark the permalink.