I am in a need of running the PHP parser for PHP code inside PHP. I am on Windows开发者_JS百科, and I have tried
system("C:\\Program Files (x86)\\PHP\\php.exe -l \"C:/Program Files (x86)/Apache/htdocs/a.php\"", $output);
var_dump($output);
without luck. The parameter -l should check for correct PHP syntax, and throw errors if some problems exist. Basically, I want to do something similar to this picture:
(source: mpsoftware.dk)That is, to be able to detect errors in code.
This seems to work:
<?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';
}
The runkit extension provides a function to do that:
runkit_lint_file()
If you can not use runkit then you only other way is to do what you already tried:
echo passthru("php -l ".__FILE__);
Try fixing your path with realpath() if it does not work.
精彩评论