ParseError
A ParseError is a subclass of the built-in Error class, itself implementing the Throwable interface, that is thrown when the PHP engine fails to parse a piece of source code because it is syntactically invalid. Unlike most runtime problems, a parse error occurs during the compilation phase, before any of the surrounding code actually executes, which historically made syntax mistakes uncatchable fatal errors; since PHP 7, wrapping the offending code in a try/catch block that targets ParseError, Error, or Throwable allows the failure to be handled gracefully instead of crashing the whole process. The most common trigger is a call to eval() on a string containing malformed PHP, but a ParseError can also surface when including or requiring a file that itself contains invalid syntax, or in a few contexts where PHP compiles code dynamically at runtime, such as certain uses of compact expressions or attribute parsing. Typical causes include mismatched brackets or quotes, missing semicolons, reserved words used as identifiers, and invalid combinations of language constructs. Because it signals a structural problem with the code itself rather than a logic error, catching ParseError is mainly useful for sandboxes, template engines, REPL, and tools that evaluate untrusted or user-supplied PHP snippets safely.
<?php
try {
eval (' A A ');
} catch (ParseError $e) {
echo "This is not PHP code";
}
?>