php
  1. php-error-reporting

PHP error_reporting

The error_reporting function in PHP is used to set the level of error reporting. It enables you to specify which errors to display and which to hide.

Syntax

The syntax of error_reporting function is:

error_reporting(level);

Here level is an optional parameter that represents the level of error reporting. It can take one of the following values:

  • 0 - Turns off error reporting
  • E_ERROR - Shows only fatal errors
  • E_WARNING - Shows warning messages
  • E_NOTICE - Shows notice messages
  • E_ALL - Shows all errors and warnings

Example

The following example demonstrates how to use the error_reporting function in PHP:

<?php
    // Turns off error reporting
    error_reporting(0);
    
    // Shows only fatal errors
    error_reporting(E_ERROR);
    
    // Shows warning messages
    error_reporting(E_WARNING);
    
    // Shows notice messages
    error_reporting(E_NOTICE);
    
    // Shows all errors and warnings
    error_reporting(E_ALL);
?>

Output

The output for each level of error_reporting would be:

  • 0 - No error messages will be displayed.
  • E_ERROR - Only fatal error messages will be displayed.
  • E_WARNING - Only warning messages will be displayed.
  • E_NOTICE - Only notice messages will be displayed.
  • E_ALL - All error messages will be displayed.

Explanation

The error_reporting function is used to set the level of error reporting in PHP. It is a very useful function when you are debugging your code and need to see what errors are being generated. You can use it to turn off error reporting altogether or to show only the most serious errors.

Use

The error_reporting function should be used whenever you need to control the level of error reporting in your PHP code. It can be used to turn off error reporting altogether or to show only the most serious errors.

Important Points

  • The error_reporting function is used to set the level of error reporting in PHP.
  • It can take one of the following values: 0, E_ERROR, E_WARNING, E_NOTICE, or E_ALL.
  • Use 0 to turn off error reporting, E_ERROR to show only fatal error messages, E_WARNING to show only warning messages, E_NOTICE to show only notice messages, and E_ALL to show all error messages.
  • Use the error_reporting function whenever you need to control the level of error reporting in your PHP code.

Summary

The error_reporting function in PHP is used to set the level of error reporting. It enables you to specify which errors to display and which to hide. You can use it to turn off error reporting altogether or to show only the most serious errors. The function should be used whenever you need to control the level of error reporting in your PHP code.

Published on: