4

I'm attempting to build an auditing feature into my application that will check for various code quality issues.

One of the things I would like to do is check certain PHP files for syntax errors. I was going to use php_check_syntax() but it has been removed in PHP 5.0.5.

I've tried using exec() statements but it isn't outputting anything. I've added a date to make sure exec() is working:

<?php

error_reporting(E_ALL | E_NOTICE | E_STRICT | E_WARNING);
ini_set('display_errors', 1);

$output = 'before';
var_dump($output);
var_dump(exec('php -l ' . __FILE__, $output));
var_dump($output);
var_dump(exec('date', $output));
var_dump($output);

Output:

string 'before' (length=6)
string '' (length=0)
array (size=0)
  empty
string 'Thu Feb  6 10:42:35 PST 2014' (length=28)
array (size=1)
  0 => string 'Thu Feb  6 10:42:35 PST 2014' (length=28)

How can I check a PHP file for syntax errors in PHP?

7
  • 2
    Did you check the command directly first? (To make sure the path is correct, and that the php command works correctly) Commented Feb 6, 2014 at 2:02
  • Make sure you're validating that path correctly. Commented Feb 6, 2014 at 2:16
  • The path is correct and the command works in terminal. I've tried /usr/bin/php as well Commented Feb 6, 2014 at 2:32
  • 1
    check php error logs and add the following to the top of php file error_reporting(E_ALL | E_NOTICE | E_STRICT | E_WARNING); @ini_set('display_errors', 1); Commented Feb 6, 2014 at 10:48
  • if it is working from the terminal and not through your web server, be sure that exec is enabled. Maybe you are running in safe mode? Commented Feb 6, 2014 at 10:57

4 Answers 4

3

check web-server configuration. places like this: disable_functions="". after var_dump($output) starts to return an array check for errors in array to eliminate further errors like correct path to php.

use this to eliminate some other possibilities:

$output="just before exec";
var_dump($output);
exec('php -l /path/to/file.php', $output);
var_dump($output);

p.s. may be used like

echo exec('php -l /path/to/file.php');

Upd: Updated question shows exec works. It may be your *nix like platform hides the error output. a way to redirect it to add to the command 2>&1 it will redirect error output into standard output

$to_run = '/path/to/bin/php -l /path/to/file 2>&1';
$output ="" ; //init
var_dump(exec($to_run, $output));

if you have root access on the platform you may even use tools like strace to debug some complicated cases.

'/path/to/bin/strace -o/path/to/strace.log php -l /path/to/file.php'

UPD: var_dump(exec('php -l ' . FILE .' 2>&1', $output)); // error redirection for a unix platform

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried this an updated my question. It's still not working.
var_dump(exec('php -l ' . FILE .' 2>&1', $output)); // error redirection for a unix platform
2

Here's a Unix bash script you could use to syntax check all .php files in folder and subfolder:

find . -iname '*.php' -exec php -l '{}' \; | grep '^No syntax errors' -v  | less

Source: https://gist.github.com/Fake51/865603

Comments

1

To check many php files, you can save this code as a batch file and execute it:

echo php files error check report >output.txt
for /r %%i in (*.php) do ( echo. & php -l %%i  )  >>output.txt
start "" "output.txt"

Save it as "check.bat" (for example) in the php files directory and execute it.

Comments

0

You can do that with lint running PHP with the -l parameter on the command line:

test.php:

<?php
phpinfo()

result:

$ php -l test.php
PHP Parse error:  syntax error, unexpected end of file in test.php on line 2
Parse error: syntax error, unexpected end of file in test.php on line 2
Errors parsing test.php

test.php

<?php
phpinfo();

result:

$ php -l test.php
No syntax errors detected in test.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.