Check your PHP file for syntax errors

I am trying to create an audit function in my application that will check various problems with the quality of the code.

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

I tried using operators exec(), but did not output anything. I added dateto make sure that it exec()works:

<?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);

Conclusion:

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 to check PHP file for syntax errors in PHP?

+3
source share
3 answers

-. : disable_functions = "". , var_dump ($ output) , , php.

, :

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

p.s.

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

: , exec. , * nix . 2 > & 1

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

root- , , strace .

'/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));// unix

+2

lint running PHP -l :

test.php

<?php
phpinfo()

:

$ 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();

:

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

php , :

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.

0
source

All Articles