Parse Errors

Wrong Syntax leads to parse errors and PHP unceremoniously aborts the execution. This is not particularly surprising as there is not really much else for PHP to do if it can’t understand the given code.

As mentioned earlier there is nothing you can do in PHP 5 to recover but registering a shutdown handler at least allows for a controlled termination. For that to work, of course, the parse error must not have occurred in the initially loaded script but in a file included or required.

With PHP 7 and Engine Exceptions the shutdown handler is no longer required. In the case of a parse error, a ParseError will be thrown, getting all relevant information along – like filename, line number, and the actual error. A ParseError, as any Error, can be handled using a try / catch block:

try {
    require '_syntax_error.php';
} catch (ParseError $parseError) {
    var_dump($parseError);
}

If a ParseError is not caught, it will translate back into a classic PHP error and will terminate the process.