Open In App

How to enable error reporting in PHP ?

In PHP, we can decide whether to show an error to end-users or not. You can enable or disable error reporting in PHP with three approaches:

Approach 1: In the php.ini file, we can set the display_error parameter as on or off. The on means errors are displayed and off means no errors to display and report. To do so, open the “php.ini” file and search for the display_error parameter and change it to value on. Save the file. Restart all services of the webserver. Now it will display all the errors of the PHP script to the end-users. To disable the error reporting, set the display_error parameter to off. The off parameter means it will not show any error to users.



Approach 2: The second approach to enable or disable error reporting is using the ini_set() function. It is an inbuilt function available in PHP to change the configuration settings available in the php.ini file. It takes two parameters, the first is the setting name you want to modify and the second is the value you want to assign.

If you want to change the value of the display_error parameter of the php.ini file



Enable the error reporting: It will enable error reporting.

ini_set("display_errors", "1") 

Disable the error reporting: It will disable the error reporting.

ini_set("display_errors", "0")

Example 1: In the code, we have enabled error reporting so it displays the error to the user. The “printname()” function is “undefined” because the name of the defined function is “printmyname()”.




<?php
   ini_set('display_errors','1');
 
  function printmyname()
  {
     echo "My name is GeeksforGeeks";
  }
 
  printname();
?>

Output:

Fatal error: Call to undefined function printname()

Example 2: The following code will turn off showing the error to users.




<?php
  ini_set('display_errors','0');
 
  function printmyname()
  {
     echo "My name is GeeksforGeeks";
  }
 
  printname();
?>

Output:

This page isn’t working. localhost is 
currently unable to handle this request.
HTTP ERROR 500

Approach 3: This approach can also be used to enable error reporting by using the error_reporting() function, which is a native PHP function that sets the error_reporting directive at runtime, i.e., it is used to sets which PHP errors are reported. This function also helps to set the different levels for the runtime (duration) of your script, as PHP contains various levels of error. This function accepts error names like E_ERROR, E_PARSE, E_WARNING, etc, as parameters, thereby allowing that specified errors to report. A few errors that occur in PHP are listed below:

Syntax:

error_reporting(error_names);

Parameter value:

Note: The error_reporting() function also accepts integer 0 which turns off all error reporting and -1 turns on all error reporting.

Example 3: The below code represents how to use the error_reporting() function in a PHP script.




<?php
    // enable reporting of specified errors
    error_reporting(E_ERROR | E_WARNING);
 
    // report all errors
    error_reporting(E_ALL);
 
    // report all errors except E_PARSE
    error_reporting(E_ALL & ~E_PARSE);
 
    // report all errors
    error_reporting(-1);
 
    // turn off all error reporting
    error_reporting(0);
    echo "Sample error reporting code using error_reporting function";
?>

Output:

Sample error reporting code using error_reporting function

Article Tags :