PHP Error Constant E_ERROR
The PHP E_ERROR Constant is a fatal run time error and E_ERROR Constant has an Integer value 1.
PHP engine halts the script execution when it arises and it logs the fatal error in error_log file. When a fatal error occurs, you may lose whatever data the program was currently processing
and it mostly occurs in memory allocation (or) host file does not exist.
Example: #1- PHP Error Constant E_ERROR
Declare a class with the duplicate name and it will throw a fatal error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod1() {
}
}
// The class has a duplicate name
class ErrorHandlingExample {
public function errorfulMethod2() {
}
}
$obj = new ErrorHandlingExample();
$obj->errorfulMethod1();
PHP Output Screen
Fatal error: Cannot declare class ErrorHandlingExample, because the name is already in use in ~\Documents\php-error-constants.php on line 14
Example: #2- PHP Error Constant E_ERROR
Try to call a wrong method name (i.e. undefined method name, which is not defined) and it will throw a fatal error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod() {
}
}
$obj = new ErrorHandlingExample();
$obj->undefinedmethod();
PHP Output Screen
Fatal error: Uncaught Error: Call to undefined method ErrorHandlingExample::wrongmethod() in ~\Documents\php-error-constants.php:14
Stack trace:
#0 {main}
thrown in ~\Documents\php-error-constants.php on line 14
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3- PHP Error Constant E_ERROR
Declare a method with the duplicate name in a class and it will throw a fatal error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod() {
// Implementation code...
}
// The method with same name of an exising method
// It will throw an fatal error
public function errorfulMethod() {
// Implementation code...
}
}
$obj = new ErrorHandlingExample();
$obj->errorfulMethod();
PHP Output Screen
Fatal error: Cannot redeclare ErrorHandlingExample::errorfulMethod() in ~\Documents\php-error-constants.php on line 14
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #4- PHP Error Constant E_ERROR
Try to concat a string in an endless loop and it will throw a fatal error Allowed memory size exhausted.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function errorfulMethod() {
$i = 0;
$str = "";
// Endless loop
while($i == 0) {
$str .= "Hello World!";
}
}
}
$obj = new ErrorHandlingExample();
$obj->errorfulMethod();
PHP Output Screen
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011720 bytes) in ~\Documents\php-error-constants.php on line 14
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #5- PHP Error Constant E_ERROR
Try to call a instance method without a passing an argument, it will throw a fatal error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class ErrorHandlingExample {
public function printMessage($message) {
echo($message);
}
}
$obj = new ErrorHandlingExample();
$obj->printMessage();
PHP Output Screen
Fatal error: Uncaught ArgumentCountError: Too few arguments to function ErrorHandlingExample::printMessage(), 0 passed in ~\Documents\php-error-constants.php on line 14 and exactly 1 expected in ~\Documents\php-error-constants.php:8
Stack trace:
#0 ~\Documents\php-error-constants.php(7): ErrorHandlingExample->printMessage()
#1 {main}
thrown 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_ERROR
Declare a method with the duplicate name in a global scope and it will throw a fatal error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
function errorfulMethod() {
// Implementation code...
}
// The method with same name of an exising method
// It will throw an fatal error
function errorfulMethod() {
// Implementation code...
}
PHP Output Screen
Fatal error: Cannot redeclare errorfulMethod() (previously declared in ~\Documents\php-error-constants.php:8) in ~\Documents\php-error-constants.php on line 14
Example: #7- PHP Error Constant E_ERROR
Try to open a database without a proper driver, it will throw a fatal error.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Open a PDO connection without enabling a PDO driver
$dbh = new PDO('mysql:host=localhost;dbname=test', "root", "password");
PHP Output Screen
Fatal error: Uncaught PDOException: could not find driver in ~\Documents\php-error-constants.php:7
Stack trace:
#0 ~\Documents\php-error-constants.php(7): PDO->__construct('mysql:host=loca...', 'root', 'password')
#1 {main}
thrown in ~\Documents\php-error-constants.php on line 7
BBMINFO