Parse
Errors in eval
uated Code
Even though the need for eval()
usually points out a
design flaw, PHP’s handling of parse errors in evaluated code has
been improved in PHP 7.
A parse error encountered in code that is to be executed via
eval()
will throw a ParseError
, exactly
like PHP would do for included or required code.
try {
eval('parse error');
echo 'next in line';
} catch (ParseError $parseError) {
var_dump($parseError);
}
The main difference compared to included code is that PHP 5
continued to run even if the execution of the eval()
failed. Since PHP 7 throws a ParseError
, the line
containing the echo
statement in the example above will
never be reached. If that line were to do some error handling or
logging, you would need to adjust it for PHP 7 by moving that line
into the catch
block.