E_PARSE (4) identifies a syntax error detected by the parser. PHP cannot execute a source file it cannot parse.
A simple example
E_PARSE
Start withA PHP file with an incomplete assignment
Do thisParse the file
ResultParse error
The parser needs a value after the equals sign. The invalid file cannot begin normal execution.
Example: #1- PHP Error Constant E_PARSE
A student is editing the message on a welcome page. Leaving the dollar sign off the variable name prevents PHP from parsing the assignment.
PHP diagnosticE_PARSE
ResultParse error: syntax error, unexpected '=' in ~\Documents\php-error-constants.php on line 7
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Missing of dollar sign in a variable
message = "Hello world";
Output
Parse error: syntax error, unexpected '=' in ~\Documents\php-error-constants.php on line 7
Advertisements
Example: #2- PHP Error Constant E_PARSE
A pupil adds a conditional to a timetable template and forgets its closing brace. The parser reaches the end of the script while the block is still open.
PHP diagnosticE_PARSE
ResultParse error: syntax error, unexpected end of file in ~\Documents\php-error-constants.php:6 on line 9
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
$i = 10;
// Missing of closing curly brace of the if statement
if($i == 0) {
Output
Parse error: syntax error, unexpected end of file in ~\Documents\php-error-constants.php:6 on line 9
Example: #3- PHP Error Constant E_PARSE
A teacher uses a switch to turn numeric ratings into display text. A missing separator after a case label prevents any rating from being displayed.
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
// Parameter without a dollar sign
public function printMessage(msg) {
echo($msg);
}
}
$obj = new ErrorHandlingExample();
$obj->printMessage('Error Handling');
Output
Parse error: syntax error, unexpected ')', expecting variable (T_VARIABLE) in ~\Documents\php-error-constants.php on line 8
Advertisements
Example: #7- PHP Error Constant E_PARSE
A club signup helper accepts more than one field. A missing comma between parameters prevents PHP from reading its declaration.
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
// Missing of comma delimiter between the parameter
public function printMessage($msg $option) {
echo($msg);
}
}
$obj = new ErrorHandlingExample();
$obj->printMessage('Error Handling', null);
Output
Parse error: syntax error, unexpected '$option' (T_VARIABLE), expecting ')' in ~\Documents\php-error-constants.php on line 9