Home > Backend Development > PHP Tutorial > Simplify debugging with PHP error_reporting

Simplify debugging with PHP error_reporting

王林
Release: 2023-08-31 10:30:01
Original
1542 people have browsed it

使用 PHP error_reporting 简化调试

Today we will learn how to use the error_reporting function for debugging in PHP.

The

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.

grammar

Let’s take a quick look at the syntax of the error_reporting function.

error_reporting(int $error_level = null): int
Copy after login

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 error
  • E_WARNING: Display runtime warning
  • E_PARSE: Display compile-time parsing errors
  • E_NOTICE: Display runtime notifications
  • E_CORE_ERROR: Displays fatal errors that occurred during PHP initial startup
  • E_CORE_WARNING: Display warnings that occurred during PHP initial startup
  • E_COMPILE_ERROR: Display fatal compile-time error
  • E_COMPILE_WARNING: Display fatal compile-time warning
  • E_USER_ERROR: Display user-generated error messages
  • E_USER_WARNING: Display user-generated warning messages
  • E_USER_NOTICE: Display user-generated notification messages
  • E_STRICT: Recommended changes to your code to ensure optimal interoperability and forward compatibility
  • E_RECOVERABLE_ERROR: Display catchable fatal errors
  • E_DEPRECATED: Show warnings about code that will not work in future releases
  • E_USER_DEPRECATED: Similar to E_DEPRECATED, but it only displays user-generated warning messages
  • E_ALL: Show all errors, warnings and notifications

Each 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.

How to useerror_reportingFunction

In 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.

Show all errors

Let’s take a quick look at the following examples.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $foo;
?>
Copy after login

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
Copy after login

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);
?>
Copy after login

E_ALL Constants are very useful for debugging the famous WSOD (White Screen of Death) error.

Show all errors except notifications

Let’s take a look at the following example.

<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 1);
echo $foo;
?>
Copy after login

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.

Show notifications and warnings

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;
?>
Copy after login

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.

in conclusion

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template