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