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.

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 errors
  • E_WARNING: display run-time warnings
  • E_PARSE: display compile-time parse errors
  • E_NOTICE: display run-time notices
  • E_CORE_ERROR: display fatal errors that occur during PHP’s initial startup
  • E_CORE_WARNING: display warnings that occur during PHP’s initial startup
  • E_COMPILE_ERROR: display fatal compile-time errors
  • E_COMPILE_WARNING: display fatal compile-time warnings
  • E_USER_ERROR: display the user-generated error message
  • E_USER_WARNING: display the user-generated warning message
  • E_USER_NOTICE: display the user-generated notice message
  • E_STRICT: suggest changes to your code which will ensure the best interoperability and forward compatibility
  • E_RECOVERABLE_ERROR: display catchable fatal errors
  • E_DEPRECATED: display warnings about code that will not work in future versions
  • E_USER_DEPRECATED: similar to E_DEPRECATED, but it display only user-generated warning messages
  • E_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.

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.

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.

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.

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.

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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to Use PHP error_reporting For Easier Debugging

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.