PHP error_reporting can help you to set the level of the error in log report and using log, you can easily identify the errors in your code.
Basically, PHP error is categorized by error levels for various kind of errors and its define all kinds of error from notice to fatal errors.The error level defines the seriousness and importance of errors.Most of the errors may be caught with a custom error handler, but some are unrecoverable.
How to use error_reporting function?
The error_reporting() function is used to set the level of PHP error and return to the current level.
You can set up an error level in php.ini. By default, PHP will report all errors except notice.Error reporting level can be set through a number of predefined constants available in PHP for errors.
Generally To set the levels of error in the PHP web application, error_reporting(), the function is useful.We can set error levels like
1 2 3 | int error_reporting(E_ERROR | E_WARNING | E_NOTICE | E_PARSE); |
How to Turn off all Errors?
To Turn off all levels of errors, we can use the same function error_reporting with value 0.
1 2 3 | error_reporting(0); |
How to Turn on all Errors?
To Turn on all levels of errors, we can use the same function error_reporting with value E_ALL which can be used “or” condition so that all types errors can be reported.
1 2 3 | error_reporting (E_ALL); // Report All Bugs |
Error Levels Types
Let’s one by one see basic types of error level.Before dive into the error levels, if you want to learn how you can log errors, learn about the LOG of PHP, MySQL and Apache here.
1. E_ERROR
This is a fatal, unrecoverable error. Examples for E_ERROR are uncaught exe or class redeclaration.It is Run-time error and it will prevent the execution of code.
1 2 3 | error_reporting(E_ERROR); |
2. E_WARNING
This is the most common kind of error. It normally occurs when something you tried and doing went wrong. Examples are missing function parameters or division by zero.
3. E_STRICT
The interesting part of this error level is the only one not included in the E_ALL constant. The Main and important reason is it is easy to convert from PHP 4 to PHP 5 and you can still run PHP 4 code in PHP 5.
4. E_NOTICE
Notices are the specifies that the code runs in the browser may be doing something unintentional like trying to read the undefined variable. It is good practice to develop with notices enabled so your code is “notice-free” before the move to live.
5. E_PARSE
Parse errors occur at the time of compiling a code and its stop the execution of code if any error occurs during parse.
6. E_CORE_ERROR
It’s an internal PHP error which occurs if any PHP extension fails to start.It occurrs in the initialization process starts.
7. E_USER_ERROR
It’s used when any user-defined error occurs and stop the execution of PHP code. User-defined errors (E_USER_*) are never caused by PHP itself.
8. E_USER_NOTICE
It’s used when any user-defined notice occurs.It is similar to E_NOTICE.
In development server, You can enable all error, but it is recommended to disable warning and notice on the production server.
These are the detailed usage of PHP error_reporting and error levels.Do you think any error levels in PHP we missed here? share your experiences with us on the comment section.