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
error_reporting(E_ERROR | E_WARNING);
error_reporting(E_ALL);
error_reporting(E_ALL & ~E_PARSE);
error_reporting(-1);
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 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 r
3 min read
How to show All Errors in PHP ?
We can show all errors in PHP using the error_reporting() function. It sets the error_reporting directive at runtime according to the level provided. If no level is provided, it will return the current error reporting level. error_reporting(E_ALL) level represents all errors, warnings, notices, etc.
3 min read
How to enable PHP support in Netbeans?
Netbeans is a free and open-source IDE for developing desktop, mobile, and web applications. It allows you to create applications in a variety of languages, including Java, HTML5, PHP, C++, etc. It is compatible with Windows, Linux, Mac OS X, and other UNIX-based operating systems. Netbeans without
2 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 authent
3 min read
How to Read and Write from Files in PHP ?
In PHP, reading from and writing to files is a common task in web development, especially for tasks such as reading configuration files, processing data files, or logging information. PHP provides built-in functions for handling file operations, making it straightforward to perform file read and wri
1 min read
How to throw an Exception in PHP?
In PHP, you can throw exceptions to indicate that an error or exceptional condition has occurred during script execution. This allows you to gracefully handle errors by interrupting the normal flow of the program and providing a mechanism for error propagation. To throw an exception in PHP, you use
2 min read
How to check if mod_rewrite is enabled in PHP ?
In PHP, there is an inbuilt function called âphpinfoâ. By using this function, we can output all the Loaded Modules and see the âmod_rewriteâ is enabled or not. Syntax: phpinfo(); Here is a process to check the âmod_rewriteâ load module is enabled or not. Note: The local server used here is XAMPP. C
1 min read
How to print out all the credits for PHP?
Given a PHP document we need to print all the credits for PHP. This can be done in a number of ways like if we want to print all the credits at once we can do it by using the flag parameter CREDITS_ALL, if we only want to print the documentation credits we can do the same by using the flag parameter
4 min read
How to Handle errors in PHP?
Handling errors in PHP involves various techniques and constructs to detect, report, and manage errors or exceptions that occur during script execution. Table of Content Error Reporting LevelError Loggingtry-catch Blocks (Exceptions)Custom Error HandlersError Reporting Level:Controls the level of er
1 min read
How to create custom errors in JavaScript ?
In this article, we will learn to create custom errors with some examples. Errors represent the state of being wrong in condition. Javascript handles a predefined set of errors by itself but if you want to create your own error handling mechanism you can do that with custom errors functionality avai
2 min read