Guide to PHP Error Reporting 2021

A PHP application produces different types of errors and warnings during the run time of the script.

In production, we don’t want to enable error so that users see any error or notice but during the development phase(staging phase) it is most required
to enable error messages to be informed about issues before we go to the production stage.

So in this post, we are going to see how to enable error reporting in PHP.

We have already talked about what is a PHP error and the types of error in PHP.

You can read here Types of Errors in PHP

How to Enable Error Reporting in PHP

It is very easy to enable error reporting in PHP using error_reporting() function.

error_reporting(level);

<?php 
// Report all errors
error_reporting(E_ALL);

//We can also report all errors by using -1
error_reporting(-1);

// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
?>

How Many Error Levels are Available in PHP?

PHP has many levels of errors and each level is represented by an integer value. So using this function we can easily set different error level reporting.

Here level can be of different types. Let us see.

E_ERROR(1)             : fatal runtime error execution of script has been halted  

E_WARNING(2)           : nonfatal runtime error execution of script has been halted

E_PARSE(4)             : The compile-time error it is generated by the parser

E_NOTICE(8)            : The script found something that might be an error

E_CORE_ERROR(16)       : Fatal errors that occurred during the initial startup of script

E_CORE_WARNING(32)     : Nonfatal errors that occurred during the initial startup of script

E_COMPILE_ERROR(64)    : Fatal error that occurs while the script was being compiled

E_COMPILE_WARNING(128) : Non-fatal error occurs while the script was being compiled.

E_USER_ERROR(256)      : Fatal user-generated error message.

E_USER_WARNING(512)    : Non-fatal user-generated warning message

E_USER_NOTICE(1024)    : User-generated notice message.

E_STRICT(2048)         : Not strictly an error

E_RECOVERABLE_ERROR(4096) : Catchable fatal error.

E_DEPRECATED(8192)        : run-time notice to indicate code will not work in future versions of PHP.

E_USER_DEPRECATED(16384)  : User-generated warning message.

E_ALL(32767)           : All errors and warning

PHP Display Errors

In PHP, you can decide whether errors should be printed on the screen or not as part of the output. Reporting errors does not mean it will display on the screen.

Displaying errors will show errors to users on-screen. We can easily turn it on.

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>

Here ini_set sets the value of a configuration option and display_errors is a string that determines whether errors should be printed to the screen or not.

display_startup_errors is used to show errors that occur during PHP’s startup sequence because even display_errors is on, errors that occur during PHP’s startup sequence are not displayed.

How to Show All PHP Errors & Warnings?

You can easily show all PHP errors by adding the below lines to your PHP files.

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>

You have learned how to show errors in PHP application. If you have any query feel free to comment.

Leave a Comment