2

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
(source: mpsoftware.dk)

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

2
  • What problem are you facing with -l option ? Commented Apr 20, 2010 at 12:44
  • I'm not getting any feedback out from php.exe... I'm not sure why. Commented Apr 20, 2010 at 14:26

2 Answers 2

2

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';   
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.