An Interest In:
Web News this Week
- February 2, 2025
- February 1, 2025
- January 31, 2025
- January 30, 2025
- January 29, 2025
- January 28, 2025
- January 27, 2025
How to Use PHP error_reporting For Easier Debugging
Today, we’ll see how you can use the error_reporting
function in PHP for debugging purposes.
The error_reporting
function allows you to configure which errors will be reported in your PHP scripts. In fact, when you use the error_reporting
function in your PHP script, it just sets the error_reporting
directive at runtime. If you’re aware of the php.ini configuration file, it provides a lot of configuration directives for different purposes, and the error_reporting
directive is one of them. Specifically, the error_reporting
directive allows you to set the error reporting level in your PHP scripts.
In this quick article, we’ll go through the basics of the error_reporting
function, and we’ll discuss how you can use it effectively in your day-to-day PHP development.
Syntax
Let’s quickly go through the syntax of the error_reporting
function.
error_reporting(int $error_level = null): int
It takes a single argument which allows you to pass the error level which you want to set. It’s an optional argument, so if you don’t pass it, it should return the current error reporting level.
You could pass either a bit-mask or named constants in this argument. However, it’s recommended to pass named constants for the compatibility for future PHP versions. Also, if you use named constants, it increases readability of your code as well.
There are different error constants that you could pass in the first argument of the error_reporting
function. Following is the quick list of all constants.
E_ERROR
: display fatal run-time errorsE_WARNING
: display run-time warningsE_PARSE
: display compile-time parse errorsE_NOTICE
: display run-time noticesE_CORE_ERROR
: display fatal errors that occur during PHP's initial startupE_CORE_WARNING
: display warnings that occur during PHP's initial startupE_COMPILE_ERROR
: display fatal compile-time errorsE_COMPILE_WARNING
: display fatal compile-time warningsE_USER_ERROR
: display the user-generated error messageE_USER_WARNING
: display the user-generated warning messageE_USER_NOTICE
: display the user-generated notice messageE_STRICT
: suggest changes to your code which will ensure the best interoperability and forward compatibilityE_RECOVERABLE_ERROR
: display catchable fatal errorsE_DEPRECATED
: display warnings about code that will not work in future versionsE_USER_DEPRECATED
: similar toE_DEPRECATED
, but it display only user-generated warning messagesE_ALL
: display all errors, warnings, and notices
Each constant allows you to set a different level of error reporting. In the next section, we’ll see how you can use the error_reporting
function in your day-to-day PHP development.
How to Use the error_reporting
Function
In the previous section, we went through the syntax of the error_reporting
function. In this section, we’ll see how you can use it in your PHP scripts.
Display All Errors
Let’s quickly go through the following example.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $foo;
?>
In the above example, we’ve passed the E_ALL
constant in the first argument of the error_reporting
function, and thus, it will display all errors, warnings and notices in our script. If you run the above script, it should display 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 throws a notice to inform you that you should define the $foo
variable before you actually use it.
Alternatively, you could also pass -1
instead of the E_ALL
constant as shown in the following snippet, and it would show every possible error.
<?php
error_reporting(-1);
?>
The E_ALL
constant is really useful to debug the famous WSOD (white screen of death) error.
Display All Errors Except Notices
Let’s go through 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 like &
, |
and ~
to omit and filter specific types of errors. In the above example, we want to display all types of errors but notices, and thus, we’ve used the ~
operator in front of the E_NOTICE
constant. If you run the above script, it won’t display the notice which it would have displayed, had you used only the E_ALL
constant.
Display Notices and Warnings
In this section, we’ll see how you can display only specific types of errors. Let’s quickly go through the following example.
<?php
error_reporting(E_WARNING | E_NOTICE);
ini_set('display_errors', 1);
include "foo_bar.php";
echo $foo;
?>
In the above example, we are instructing the error_reporting
function that we want to display only warnings and notices. As you can see, we’ve used the |
operator, so it would display both types of errors.
So that’s how you can use the error_reporting
function with different types of error constants for debugging purposes in your day-to-day PHP development.
Conclusion
Today, we discussed how you can use the error_reporting
function in PHP to debug errors in your PHP scripts. We discussed how you can use it to display different levels of errors during development.
Original Link: https://code.tutsplus.com/tutorials/how-to-use-php-error_reporting-for-easier-debugging--cms-37986

TutsPlus - Code

More About this Source Visit TutsPlus - Code