PHP Error Constant E_PARSE
The PHP E_PARSE Constant is a fatal compile time error and E_PARSE Constant has an Integer value 4.
PHP engine doesn't start the script execution when it arises and it logs the parse error in error_log file. It mostly occurs in syntax error.
Example: #1- PHP Error Constant E_PARSE
Declare and initialize a variable without a dollar sign, it will throw a compile time parse error.
PHP Input Screen
<?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";
PHP Output Screen
Parse error: syntax error, unexpected '=' in ~\Documents\php-error-constants.php on line 7
Example: #2- PHP Error Constant E_PARSE
Write a code with a conditional if statement without a closing curly brace, it will throw a compile time parse error.
PHP Input Screen
<?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) {
PHP Output Screen
Parse error: syntax error, unexpected end of file in ~\Documents\php-error-constants.php:6 on line 9
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3- PHP Error Constant E_PARSE
Write a code with a conditional switch statement without a colon on case statement, it will throw a compile time parse error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
$rating = 1;
switch($rating) {
// Missing of colon
case 1
echo('Very Poor');
break;
case 2:
echo('Poor');
break;
case 3:
echo('Average');
break;
case 4:
echo('Good');
break;
case 5:
echo('Excellent');
break;
}
PHP Output Screen
Parse error: syntax error, unexpected 'echo' (T_ECHO) in ~\Documents\php-error-constants.php on line 11
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #4- PHP Error Constant E_PARSE
Write a code print the text using a echo statement without an argument, it will throw a compile time parse error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// callaing a echo function without an argument
echo();
PHP Output Screen
Parse error: syntax error, unexpected ')' in ~\Documents\php-error-constants.php on line 7
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #5- PHP Error Constant E_PARSE
Assign a value to a variable without an semi colon at the end of the statement, it will throw a compile time parse error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Assignment statement without a semi colon
$msg1 = "Hello"
$msg2 = "John";
PHP Output Screen
Parse error: syntax error, unexpected '$msg2' (T_VARIABLE) in ~\Documents\php-error-constants.php on line 8
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #6- PHP Error Constant E_PARSE
Declare a class method with a parameter without a dollar sign, it will throw a compile time parse error.
PHP Input Screen
<?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');
PHP Output Screen
Parse error: syntax error, unexpected ')', expecting variable (T_VARIABLE) in ~\Documents\php-error-constants.php on line 8
Example: #7- PHP Error Constant E_PARSE
Declare a class method with a couple of parameter without a delimiter comma, it will throw a compile time parse error.
PHP Input Screen
<?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);
PHP Output Screen
Parse error: syntax error, unexpected '$option' (T_VARIABLE), expecting ')' in ~\Documents\php-error-constants.php on line 9
BBMINFO