PHP Error Handling

Error Reporting

// Report all PHP errors.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

This alone doesn’t make PHP show parse errors. The only way to show parse errors is to set display_errors = on in the php.ini file.

trigger_error()

  • trigger_error() can be used to trigger a PHP notice, warning, or fatal error.
  • By default a NOTICE is triggered, but the second parameter can set the error level.

trigger_error($error_msg, $error_type);

trigger_error('Test notice!', E_USER_NOTICE);   // Trigger a notice.
trigger_error('Test warning!', E_USER_WARNING); // Trigger a warning.
trigger_error('Test error!', E_USER_ERROR);     // Trigger a error.

Error Handling

Error handling is the process of catching errors raised by your program and then taking an appropriate action.

Handling fatal errors in the past has been next to impossible in PHP. A fatal error would not invoke the error handler set by set_error_handler() and would simply halt script execution.

In PHP 7, an exception will be thrown when a fatal and recoverable error (E_ERROR and E_RECOVERABLE_ERROR) occurs, rather than halting script execution.
Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately halting script execution. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an exception thrown from an error that was fatal in PHP 5.x goes uncaught, it will still be a fatal error in PHP 7.

Usually an instance of the base Error class is thrown from previously fatal errors, but some errors will throw a more specific subclass of Error: TypeError, ParseError, and AssertionError.

Error Levels

Level Constant Name Error Type o Calls Cases
1 E_ERROR Fatal Error “It’s a Fatal Run Time Error..
It halts the Script execution.
E_ERROR Constant has an Integer value 1.
E_ERROR mostly occurs in Memory allocation (or) Host file does not exist.
It can’t be recovered by any Error Handling (or) Exception Handling techniques.
By default E_ERROR is logged in error_log file.”
SHUTDOWN “Call to Undefined Function
Endless Loop
Endless Dynamic Concatenation of String (or) Endless Dynamic Manipulation
Creation of Undefined Class Object
Redeclaration of an existing Class
Redeclaration of an existing Function”
2 E_WARNING Warning “It’s a Non Fatal Run Time Error.
It doesn’t halt the Script execution.
E_WARNING Constant has an Integer value 2.
E_WARNING mostly occurs in Failure of outsource.
It can be recovered by Error Handling (or) Exception Handling techniques.
By default E_WARNING is logged in error_log file.”
ERROR “Unknown File Opening (or) Accessing
MySQL or any other Database – User Authentication Failed
More than one Header Information sent to the PHP Engine, etc.,”
4 E_PARSE Parse error “It’s not a Fatal Error..
It doesn’t begin Script execution while on Parsing returns any error.
E_PARSE Constant has an Integer value 4.
E_PARSE mostly occurs in Syntax error.
It can’t be recovered by any Error Handling (or) Exception Handling techniques.
By default E_PARSE is logged in error_log file.”
CRASH “Missing of Semi Coloun
Missing of Curly Braces
Missing of Dollar Sign in fornt of a Variable
Missing of Square Bracket in an Array
Missing (or) Wrongly spelled Hot Keywords
Missing of PHP ending tag”
8 E_NOTICE Other “It’s a Non Fatal Run Time Notice.
It doesn’t halt the Script execution.
E_NOTICE Constant has an Integer value 8.
E_NOTICE mostly occurs in un-initalized usage of Variables.
It can be recovered by Error Handling (or) Exception Handling techniques.
By default E_NOTICE is logged in error_log file.”
ERROR “Un-Initialized Variable Usage
Un-Initialized Server Variable Usage
Un-Initialized Constant Usage”
16 E_CORE_ERROR Fatal Error “It’s a Fatal Run Time Error.
E_CORE_ERROR is generated by PHP Core Enging on Intial Starup.
It halts the Script execution.
E_CORE_ERROR Constant has an Integer value 16.
By default E_CORE_ERROR is logged in error_log file.
It’s simillar to E_ERROR.”
? Hard to reproduce. Can only be raised by PHPs core Zend engine.
32 E_CORE_WARNING Warning “It’s a Non-Fatal Run Time Warning.
E_CORE_WARNING is generated by PHP Core Enging on Intial Starup.
It doesn’t halts the Script execution.
E_CORE_WARNING Constant has an Integer value 32.
By default E_CORE_WARNING is logged in error_log file.
It’s simillar to E_WARNING.”
? Hard to reproduce. Can only be raised by PHPs core Zend engine.
64 E_COMPILE_ERROR Fatal Error “It’s a Fatal Run Time Error.
E_COMPILE_ERROR is generated by Zend Engine.
It halts the Script execution.
E_COMPILE_ERROR Constant has an Integer value 64.
By default E_COMPILE_ERROR is logged in error_log file.
It’s simillar to E_ERROR.”
? Hard to reproduce. Can only be raised by PHPs core Zend engine.
128 E_COMPILE_WARNING Warning “It’s a Non-Fatal Run Time Warning.
E_COMPILE_WARNING is generated by Zend Engine.
It doesn’t halts the Script execution.
E_COMPILE_WARNING Constant has an Integer value 128.
By default E_COMPILE_WARNING is logged in error_log file.
It’s simillar to E_WARNING.”
? Hard to reproduce. Can only be raised by PHPs core Zend engine.
256 E_USER_ERROR Fatal Error “It’s a User-generated Fatal Run Time Error..
It halts the Script execution by default except by triggering PHP set_error_handler() with catchable Error Type.
E_USER_ERROR Constant has an Integer value 256.
E_USER_ERROR mostly triggered in Memory allocation (or) Host file does not exist.
It can be called by Error Handling (or) Exception Handling techniques.
In case any common PHP Error is triggered by the PHP Engine during execution, the Custom User Error cannot be executing at this time.
By default E_USER_ERROR is logged in error_log file.”
ERROR “Call to Undefined Function
Endless Loop
Endless Dynamic Concatenation of String (or) Endless Dynamic Manipulation
Creation of Undefined Class Object
Redeclaration of an existing Class
Redeclaration of an existing Function”
512 E_USER_WARNING Warning “It’s a User-generated Run Time Warning (Non Fatal).
It doesn’t halt the Script execution.
E_USER_WARNING Constant has an Integer value 512.
E_USER_WARNING mostly triggered in Failure of outsource.
It can be called by Error Handling (or) Exception Handling techniques.
In case any common PHP Error is triggered by the PHP Engine during execution, the Custom User Error cannot be executing at this time.
By default E_USER_WARNING is logged in error_log file.”
ERROR “Unknown File Opening (or) Accessing
MySQL or any other Database – User Authentication Failed
More than one Header Information sent to the PHP Engine, etc.,”
1024 E_USER_NOTICE Other “It’s a User-generated Run Time Notice (Non Fatal).
It doesn’t halt the Script execution.
E_USER_NOTICE Constant has an Integer value 1024.
E_USER_NOTICE mostly triggered in un-initalized usage of Variables.
It can be called by Error Handling (or) Exception Handling techniques.
In case any common PHP Error is triggered by the PHP Engine during execution, the Custom User Error cannot be executing at this time.
By default E_USER_NOTICE is logged in error_log file.”
ERROR “Un-Initialized Variable Usage
Un-Initialized Server Variable Usage
Un-Initialized Constant Usage”
2048 E_STRICT Other “It’s a Run Time Suggestion Information given by PHP Engine to the developer.
It doesn’t halt the Script execution.
It is also known as Strict Standards.
E_STRICT Constant has an Integer value 2048.
E_STRICT mostly occurs in Accessing static property.
By default E_STRICT is logged in error_log file.”
N/A “Accessing unappropriate Static Property
Passing Argument by reference
Generic Object Property is Unset, etc.,”
4096 E_RECOVERABLE_ERROR Recoverable fatal error “It’s a Catchable Run Time Error (Fatal Error).
It halts the Script execution by default except by triggering PHP set_error_handler() with catchable Error Type.(Refer Example #1 & #2)
It’s a previous stage of PHP E_ERROR, this error alerts the User that the PHP Engine decides that something is wrong and end up to an unstable state.
E_RECOVERABLE_ERROR Constant has an Integer value 4096.
E_RECOVERABLE_ERROR mostly occurs in Type Hinting
In PHP, Type-Casting in only supported for Non-Scalar Elements like Class Objects, Arrays and Resources not for Scalar – int, float, string, etc.,
Scalar Type-Casting is a BAD practice.
It can be recovered by any Error Handling (or) Exception Handling techniques.
By default E_RECOVERABLE_ERROR is logged in error_log file.”
SHUTDOWN Scalar Type Casting in Function Declaration.
8192 E_DEPRECATED Other “It indicates that a function or feature has been deprecated.
It doesn’t halt the Script execution.
E_DEPRECATED Constant has an Integer value 8192.
E_DEPRECATED mostly occurs in call to Depricated Functions like split(), ereg(), etc.,.
By default E_DEPRECATED is logged in error_log file.”
SHUTDOWN
16384 E_USER_DEPRECATED Other “It indicates that a function or feature has been deprecated.
It doesn’t halt the Script execution.
E_USER_DEPRECATED Constant has an Integer value 16384.
E_USER_DEPRECATED mostly occurs in call to Depricated Functions like split(), ereg(), etc.,
It can be called by Error Handling (or) Exception Handling techniques.
In case any common PHP Error is triggered by the PHP Engine during execution, the Custom User Error cannot be executing at this time..
By default E_USER_DEPRECATED is logged in error_log file.”
ERROR
32767 E_ALL “All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0.
It’s a Common Error Label for all Error Types.
E_ALL Constant has a different Integer values according to the PHP Version.
By default E_ALL is logged in error_log file.
It’s mostly used in the PHP set_error_handler() and error_reporting() function.”
N/A

// Fatal errors
– Not invoke the error handler.
– Abort execution in the current context
– Directly jump (bailout) to the shutdown procedure.
E_ERROR
E_CORE_ERROR
E_COMPILE_ERROR
E_USER_ERROR

// Recoverable fatal errors
E_RECOVERABLE_ERROR
// Parse error
E_PARSE

// Warnings
E_WARNING
E_CORE_WARNING
E_COMPILE_WARNING
E_USER_WARNING

// Notices etc.
E_DEPRECATED
E_USER_DEPRECATED
E_NOTICE
E_USER_NOTICE
E_STRICT

Resources