PHP ParseError

PHPServer Side ProgrammingProgramming

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

 Live Demo

<?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 ';'
raja
Published on 21-Sep-2020 14:51:55
Advertisements