How can I check the syntax of a PHP file in PHP?
I need to run a PHP parser for PHP code inside PHP. I am on Windows and I tried
system("C:\\Program Files (x86)\\PHP\\php.exe -l \"C:/Program Files (x86)/Apache/htdocs/a.php\"", $output);
var_dump($output);
no luck The -l option should check for valid PHP syntax and throw errors if there are some problems. Basically, I want to do something similar to this picture:
(source: mpsoftware.dk )
That is, to be able to detect errors in the code.
+2
a source to share
2 answers
It works:
<?php
$file = dirname(__FILE__) . '/test.php';
//$output will be filled with output from the command
//$ret will be the return code
exec('php -l ' . escapeshellarg($file), $output, $ret);
//return code should be zero if it was ok
if ($ret != 0) {
echo 'error:';
var_dump($output);
} else {
echo 'ok';
}
+2
a source to share
The runkit extension provides a function for this:
If you can't use runkit, then you can only use what you've already tried:
echo passthru("php -l ".__FILE__);
Try to fix your path with realpath () if it doesn't work.
+1
a source to share