PHP Errors
- Error handling is the core part of any programming language, So PHP also provided error handling methods to handle small to large any type errors.
- In many programming languages, an error is a failure or broken stage of the program defines as an error, These errors may be run-time errors or configuration errors.
- PHP serves various types of errors such as i.e NOTICE, WARNING & FATAL errors, etc. So, we will learn in this chapter that how is PHP reports errors of each type of errors.
PHP provides several types of errors as per conditions, these errors displays by using predefined error constants, there the list of all error types:
Value |
Constant |
Description |
Note |
---|---|---|---|
1 |
E_ERROR |
Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. The execution of the script is halted. |
|
2 |
E_WARNING |
Run-time warnings (non-fatal errors). The execution of the script is not halted. |
|
4 |
E_PARSE |
Compile-time parse errors. Parse errors should only be generated by the parser. |
|
8 |
E_NOTICE |
Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. |
|
16 |
E_CORE_ERROR |
Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP. |
|
32 |
E_CORE_WARNING |
Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP. |
|
64 |
E_COMPILE_ERROR |
Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine. |
|
128 |
E_COMPILE_WARNING |
Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine. |
|
256 |
E_USER_ERROR |
User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error(). |
|
512 |
E_USER_WARNING |
User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error(). |
|
1024 |
E_USER_NOTICE |
User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error(). |
|
2048 |
E_STRICT |
Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. |
Since PHP 5 but not included in E_ALL until PHP 5.4.0 |
4096 |
E_RECOVERABLE_ERROR |
Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user-defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR. |
Since PHP 5.2.0 |
8192 |
E_DEPRECATED |
Run-time notices. Enable this to receive warnings about code that will not work in future versions. |
Since PHP 5.3.0 |
16384 |
E_USER_DEPRECATED |
User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error(). |
Since PHP 5.3.0 |
32767 |
E_ALL |
All errors and warnings, as supported, except level E_STRICT prior to PHP 5.4.0. |
|
PHP has been used error handling in php.ini run-time configuration file, There are It uses pre-configured error handler error_reporting. If there are not set any error handler then you can set error handling in It’s php.ini.
If you want to set an error handler at run-time of the program or at the line of code, It provided error_reporting() function to handle this.
The error_reporting() function accepts parameter as integer value either ‘0’ or ‘1’ otherwise It uses error’s predefined constants.
Example-1:
<?php
/** It will be return errors **/
error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED );
?>
But, in many cases, E_ALL is mostly used to show up every type of errors.
Note:
How to turn On/Off errors in PHP
Yes, It’s possible. You may set errors state on-to-off & off-to-on. To do this you will be set integer value either 0 or 1 as per conditions.
Let us consider an example of error On/Off:
<?php
/** It indicates that error reporting is turned on **/
error_reporting(1);
/** It indicates that error reporting is turned off, and any errors will be not show during **/
error_reporting(0);
?>
PHP 7.0.0+ Errors
Before PHP 7, It uses traditional mechanisms to report errors. but now in PHP 7 or later I using Throwing Error exception.
They are using try, throw & catch statement to produce errors, It is the fully object-orient approach to error handling. So, there are given some predefined error handling classes.
-
Error - Error is the base class for all internal PHP errors.
-
Throwable - Throwable is the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception.
-
Exception - Exception is the base class for all Exceptions in PHP 5, and the base class for all user exceptions in PHP 7.