The Wayback Machine - https://web.archive.org/web/20240718094626/https://www.geeksforgeeks.org/how-to-enable-error-reporting-in-php/
Open In App

How to enable error reporting in PHP ?

Last Updated : 11 Sep, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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




<?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




<?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:

  • E_PARSE: The compile-time error generated by the parser.
  • E_ERROR: Fatal runtime error-execution of script get’s halted.
  • E_WARNING: Non-fatal runtime error-execution of script get’s halted.
  • E_CORE_ERROR: Fatal errors occur during the initial startup of the script.
  • E_CORE_WARNING: Non-fatal errors occur during the initial startup of the script.
  • E_NOTICE: The script found something that might be an error.
  • E_ALL: Includes all errors and warnings.

Syntax:

error_reporting(error_names);

Parameter value:

  • error_names: It specifies the name of errors that will pass as an argument.

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




<?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


Similar Reads

How to create a reporting app with Vue 3 and Composition API ?
Vue is an open-source JavaScript front-end framework for building the UI for websites and apps. The main features are an incrementally adaptable architecture that focuses on declarative rendering and component composition. Not long time ago Vue has released new version - Vue 3. The updated framework has some new features, in particular Composition
3 min read
How to retrieve error message using Exception class in PHP when error occurred ?
Exception handling is the important part of PHP in which we deal with how to maintain the flow of the program if there is an error or exception that occurs during the compilation. As you know we can handle the exception by throw, try-catch, die method so now we understand how to retrieve the error message using exception class by given below exampl
2 min read
Difference between throw Error('msg') and throw new Error('msg')
The throw statement allows you to create an exception or a custom error. The exception can be like a Javascript string, a number, a boolean, or an object. So, If you use this statement together with the try...catch statement. It allows you to control the flow of the program and generate accurate error messages. For example: throw "too small" (throw
3 min read
Difference between Type Error and Reference Error in JavaScript
JavaScript is one of the most popular programming languages in the world, and it is used by millions of developers to create dynamic and interactive web applications. However, like any programming language, JavaScript is not without its quirks and pitfalls. Two common issues that developers often encounter when working with JavaScript are Type Erro
6 min read
Error Handling with Error Boundary Hooks
In the world of web development handling errors effectively is crucial for providing a seamless user experience. React one of the most popular JavaScript libraries provides a powerful feature known as Error Boundary Hooks that allows developers to handle errors in their applications. In this article, we will explore how Error Boundary Hooks can be
2 min read
How to Fix "macOS: xcrun: error: invalid active developer path, missing xcrun" error?
Encountering the "macOS: xcrun: error: invalid active developer path, missing xcrun" error can be frustrating, especially when you need to get development tasks done swiftly. This error commonly occurs when the Xcode Command Line Tools are not properly installed or when their path is corrupted. Before diving into the solutions, it’s important to un
2 min read
Express Returns Servers HTML Error Page Instead of res.json Error Message
When building APIs with Express.js, you might encounter a situation where instead of returning a JSON error message, your server returns an HTML error page. This can be frustrating, especially when your application relies on consistent JSON responses. In this article, we'll explore why this happens and how to ensure that Express.js returns a JSON e
4 min read
How to enable PHP's openssl extension to install Composer ?
To perform the secure HTTPS transfers you will need to enable the openssl extension then you have to install the composer. In this article, we will cover the enabling openssl for XAMPP as well as WAMP one by one then we will install the composer. Enable openssl extension on XAMPP Enable openssl extension on WAMP Installation of Composer Enable PHP’
3 min read
How to enable cURL in PHP?
Often, web applications require HTTP based UserID and Password authentication, cookies, and form uploads. Even, user authentication with Google or Facebook sign-in is done via HTTP. In these types of cases, we need to request a particular service server(Like Google's) for user validation and authentication token on our server. The entire process ta
3 min read
Bootstrap 5 Enable Tooltips Everywhere
Bootstrap 5 Tooltips can be enabled everywhere by first creating a list of all the tooltips and then enabling each of them. All the tooltips have an attribute named "data-bs-toggle" set to "tooltip". This attribute can be used to select all the tooltips using the document.querySelectorAll() method. Bootstrap 5 Enable Tooltips Everywhere Classes: Th
2 min read
three90RightbarBannerImg