4

Is it possible to check syntax of php-code (without running), similar to the

php-cli -l

when running php the "ordinary" way (as a module)?

0

4 Answers 4

2

There are also some PECL extensions which parse PHP code for various reasons. First there is BCompiler, which can compile a PHP into byte code. Since this step requires parsing the PHP code, I would expect errors if it isn't lint. Parsekit allwos you to compile PHP code to OP codes, which is basically what you desire. However, the extension did not have a release since late 2009, so it might be outdated. Parse_Tree is sadly unmaintained since 2007, but its purpose is to parse a PHP file into an AST. Maybe you can get to something with this one, after some polishing.

PHP_Parser is a PEAR package, which does not rely on special PHP extensions and attempts to parse PHP code from within PHP. Its marked alpha and unmaintained, but it might give you a basis to experiment with.

Sign up to request clarification or add additional context in comments.

3 Comments

Then you'll probably end up writing it on your own. :)
request of reopening php_check_syntax filed at php.net.
contact with dev at php.net. check_syntax was removed due to execution of require:ed scripts. Closing this for now.
1

You can try to run a tool like PHP Depend on the sources, which attempts to parse the given PHP files into an abstract syntax tree. While this might not catch all PHP parser errors, it will already catch quite a lot of them.

You get nice software metrics as an additional goodie, if the code is valid. :)

1 Comment

Thanks for the info, however, for the purpose, I'd prefer a more dead simple solution.
0

Is it possible to check syntax of php-code (without running), similar to the php-cli -l when running php the "ordinary" way (as a module)?

I think the question everyone missed is that there is no difference in PHP syntax whether you run it as a module, or simply execute the binary from a shell: the PHP syntax is the same on both occasions. So, you might as well just use php -l filename.php, as that has the exact same result as using the tools listed above.

1 Comment

well aware of that, however, not having to rely on installed php-cli would be a benefit.
-1

For getting the same result as php-cli -l, use the function: php_check_syntax

6 Comments

(PHP 5 <= 5.0.4) "For technical reasons, this function is deprecated and removed from PHP." It also executes the file, but OP states "without running".
-1. Look at the docs. that function was removed in 5.0.5: us.php.net/php_check_syntax
php_check_syntax executes the code. But the comments on php.net/php_check_syntax are worth a read.
You are right, guys, I didn't noticed that it was deprecated. I guess then a good option is to make system call to 'php-cli -l', if you are able to.
It's not deprecated. It doesn't exist. it was removed! Trying to call it will cause a missing function error...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.