7

Is there a PHP validator like there is an HTML validator at w3.org?

  • 6
    To validate what? PHP’s syntax? You will get a parsing error if the syntax is invalid. – Gumbo Sep 20 '09 at 14:01
  • I think he's on the lookout for something like "lint for PHP". That question has been asked here before: see http://stackoverflow.com/questions/378959/is-there-a-static-code-analyzer-like-lint-for-php-files in particular, PHPlint (http://www.icosaedro.it/phplint/) looks promising. – bart Apr 10 '10 at 16:55

5 Answers5

17

You can validate the syntax without running a PHP script itself, using php from the command line, with the option "-l" :

$ php --help 
Usage: php [options] [-f] <file> [--] [args...]
       php [options] -r <code> [--] [args...]  
       php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
       php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
       php [options] -- [args...]
       php [options] -a
  ...
  -l               Syntax check only (lint)
  ...

For example, with a file that contains :

<?php

,


die;

?>

(Note the obvious error)

You'll get :

$ php -l temp.php
PHP Parse error:  syntax error, unexpected ',' in temp.php on line 3

Parse error: syntax error, unexpected ',' in temp.php on line 3
Errors parsing temp.php


Integrating this in a build process, or as a pre-commit SVN hook, is nice, btw : it helps avoiding having syntax errors in production ^^

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
9

You can run php with the -l or --syntax-check flag. It checks the syntax of the supplied file without actually running it

php --syntax-check myfile.php
Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
2

PHP engine itself.

To turn on error messages:

error_reporting(E_ALL);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

Building on what others have said:

error_reporting(E_ALL);

Simply using PHP's own error messages is good enough. However, if you really want to get anal and use a set "standard" you can opt for a PHP Code Sniffer, which for example you can implement as pre-commit hooks to your version control system.

Here's a SO question which explains their usefulness: How useful is PHP CodeSniffer? Code Standards Enforcement in General?

Community
  • 1
  • 1
KushalP
  • 10,976
  • 6
  • 34
  • 27
0

You might try this one:

https://phpcodechecker.com/

It seems to do a fair job of it. Gets confused about curly braces within strings, but pretty handy.

Dave R.
  • 1
  • 1