This is a quick “now you know” article showing you how to validate your PHP code using PHP from the command line instead of using an online PHP validator.
Online PHP Validators
So, you’ve got some PHP code, and you want to check that it’s valid—a pretty common scenario.
Maybe there’s a bug in it you can’t find; maybe your web host doesn’t allow you to view errors to debug.
You probably just search for ‘PHP Validator’ and paste your code into the text box on whatever the first result is and see what comes out.
Seems sensible right?
But where does that pasted code go? It’s more than likely it’s being stored somewhere, by someone, for a variety of reasons – basic ones like caching, but it could be malicious – websites that let you paste code for validation that may later scan that code for passwords or URLs to be later used in an attack.
Validating PHP Code from the Command Line
Simply run PHP on the command line followed by the -l (lint) option and the path to a PHP file, and it will validate the PHP code within:
php -l /path/to/your/file.php
If nothing is returned, your syntax is OK! No need to release your code into the hands of persons unknown.
The Official PHP docs have more information on using PHP from the command line.