Check PHP syntax error in PHP.

This sounds much like “Inception” right?

Well a week ago I had the need to implement a PHP syntax check in PHP, to be more precise inside a PrestaShop (PS) module.

After a little bit of “googling” I stumbled upon a StackOverflow comment showing the solution I needed. Actually I missed the original link so I’m posting this pearl as a “bump” just to help spread the solution. I don’t own any credits 😉

Here’s it is the magic statement:

exec(sprintf('echo %s | php -l', escapeshellarg($string)), $output, $exit);

All this instruction does is to invoke the PHP “lint“ functionality and collect its results.

Just for the record here there is the method of the over mentioned module:

private function checkPhpSyntaxErrors($string)
   exec(sprintf('echo %s | php -l', escapeshellarg($string)), $output, $exit);
    if ($exit !== 0) {
      $this->_errors[] = $this->l("Syntax error: {$this->outputVarDump($output)}");
      return false;
    return true;

I hope this can be helpful to everyone facing the same problem as mine.

To the next time!


Leave a Reply

Please Login to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.