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
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.
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.
error_reporting (E_ALL); // Report All Bugs
Error Levels Types
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.
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.
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.
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.
Parse errors occur at the time of compiling a code and its stop the execution of code if any error occurs during parse.
It’s an internal PHP error which occurs if any PHP extension fails to start.It occurrs in the initialization process starts.
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.
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.