Home » Programming » PHP » Php Validator

PHP Validator – Safely Checking Your PHP Code Syntax Quickly and Easily

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.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment