How to enable error reporting in PHP ?
Last Updated :
11 Sep, 2022
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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read