Home >Backend Development >PHP Tutorial >Simplify debugging with PHP error_reporting
Today we will learn how to use the error_reporting
function for debugging in PHP.
error_reporting
function allows you to configure which errors are reported in PHP scripts. In fact, when you use the error_reporting
function in a PHP script, it just sets the error_reporting
directive at runtime. If you know about the php.ini configuration file, it provides many configuration directives for different purposes, the error_reporting
directive is one of them. Specifically, the error_reporting
directive allows you to set the error reporting level in PHP scripts.
In this short article, we will cover the basics of the error_reporting
function and will discuss how to use it effectively in daily PHP development.
Let’s take a quick look at the syntax of the error_reporting
function.
error_reporting(int $error_level = null): int
It takes a parameter that allows you to pass the error level you want to set. It's an optional parameter, so if you don't pass it, it should return the current error reporting level.
You can pass a bitmask or named constant in this parameter. However, it is recommended to pass named constants for compatibility with future PHP versions. Also, if you use named constants, it also improves the readability of your code.
You can pass different error constants in the first parameter of the error_reporting
function. Below is a quick list of all constants.
E_ERROR
: Display fatal runtime errorE_WARNING
: Display runtime warningE_PARSE
: Display compile-time parsing errorsE_NOTICE
: Display runtime notificationsE_CORE_ERROR
: Displays fatal errors that occurred during PHP initial startupE_CORE_WARNING
: Display warnings that occurred during PHP initial startupE_COMPILE_ERROR
: Display fatal compile-time errorE_COMPILE_WARNING
: Display fatal compile-time warningE_USER_ERROR
: Display user-generated error messagesE_USER_WARNING
: Display user-generated warning messagesE_USER_NOTICE
: Display user-generated notification messagesE_STRICT
: Recommended changes to your code to ensure optimal interoperability and forward compatibilityE_RECOVERABLE_ERROR
: Display catchable fatal errorsE_DEPRECATED
: Show warnings about code that will not work in future releasesE_USER_DEPRECATED
: Similar to E_DEPRECATED
, but it only displays user-generated warning messagesE_ALL
: Show all errors, warnings and notificationsEach constant allows you to set different levels of error reporting. In the next section, we will see how to use the error_reporting
function in daily PHP development.
error_reporting
FunctionIn the previous section, we learned about the syntax of the error_reporting
function. In this section, we will see how to use it in PHP scripts.
Let’s take a quick look at the following examples.
<?php error_reporting(E_ALL); ini_set('display_errors', 1); echo $foo; ?>
In the above example, we passed the E_ALL
constant in the first parameter of the error_reporting
function, therefore, it will display all the errors, warnings and notifications in the script . If you run the above script, it should show the following error.
Notice: Undefined variable: foo in /web/demo/error_reporting.php on line 4
Since we are using the $foo
variable without defining it beforehand, it will throw a notification that you should define the $foo
variable before actually using it.
Alternatively, you can pass -1
instead of the E_ALL
constant, as shown in the following code snippet, which will display all possible errors.
<?php error_reporting(-1); ?>
E_ALL
Constants are very useful for debugging the famous WSOD (White Screen of Death) error.
Let’s take a look at the following example.
<?php error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', 1); echo $foo; ?>
When you use the error_reporting
function, you can use operators such as &
, |
, and ~
to ignore and filter specific Type error. In the above example, we want to display all types of errors except notifications, therefore, we use the ~
operator in front of the E_NOTICE
constant. If you run the above script, it does not show the notification that it should show if you only use the E_ALL
constant.
In this section we will see how to display only specific types of errors. Let's take a quick look at the following examples.
<?php error_reporting(E_WARNING | E_NOTICE); ini_set('display_errors', 1); include "foo_bar.php"; echo $foo; ?>
In the above example, we instructed the error_reporting
function that we want only warnings and notifications to be displayed. As you can see, we used the |
operator so it shows both types of errors.
This is how to use the error_reporting
function and different types of error constants for debugging in daily PHP development.
Today, we discussed how to use the error_reporting
function in PHP to debug errors in PHP scripts. We discussed how it can be used to display errors at different levels during development.
The above is the detailed content of Simplify debugging with PHP error_reporting. For more information, please follow other related articles on the PHP Chinese website!