Catching PHP Parser error when using include

php, try-catch

Solution

Parser errors are fatal errors, so you can't `catch` them. See this question and answer for more details.

What you can do if you can run `exec()` is call `php -l thefilename.php` and check the result. See the manual for information on how this works. There are a few problems here, however:

- This is extremely dangerous, because you are passing information to the command line. You absolutely must filter any user input very carefully, or you would be giving the user very broad access to your system.

- `exec()` is often disabled, as it should be, because of the extremely high security risks of using it incorrectly.

- There's really no good reason to include a file that you haven't already validated for syntax errors. If this is for plugins or something, then I understand your reasoning. If it is code you have control over, however, you should validate before putting it into production.

Problem

I have a file called `functions.php`. This file consists includes to all the other function files, for example: ``` include_once("user_functions.php"); include_once("foo_functions.php"); ``` I would like to catch errors where when I screw a code in one of those files, It wouldn't give the error to the entire system. For example, if there is a parser error in `foo_functions.php` it will just not include it in `functions.php`. Is that possible?

Original source

Related problems