Computer >> Computer tutorials >  >> Programming >> PHP

PHP ParseError


Introduction

ParseError class extends CompileError class. (Earlier it used to be subclass of Error class). This type of error is thrown while a PHP code inside a string that is given to eval() function as argument.

The eval() function evaluates given string as PHP code.

Syntax

eval ( string $code ) : mixed

Parameters

Sr.NoParameter & Description
1code
valid PHP code to be evaluated

Code to be evaluated must not be embedded in PHP opening and closing tags and must be terminated by semicolon. Valide code retuns NULL whereas error in code throws ParseError

Following example throws ParseError and is handled by catch block

Example

<?php
$a=10;
try{
   eval('$a=$a+;');
}
catch (ParseError $e){
   echo "Parse Error:" . $e->getMessage();
}
?>

Output

This will produce following result −

Parse Error:syntax error, unexpected ';'