views:

47

answers:

2

Hi,

I am in a need of running the PHP parser for PHP code inside PHP. I am on Windows, 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:

alt text

That is, to be able to detect errors in code.

+1  A: 

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.

Brutos
+2  A: 

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';   
}
Tom Haigh