php error reporting

How to Enable PHP Error Reporting

It is important to report different errors incurred on your PHP website to be able to debug them effectively. However, in most cases, the PHP websites remain silent whenever errors occur. In this article, we will look at how to enable PHP error reporting to display all errros. We will also look at the different types of error reporting supported in PHP.


How to Enable PHP Error Reporting

The easiest and most common way to enable error reporting in PHP is to add the following lines of code to your PHP file.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Let us look at each of the above lines. The first line uses ini_set function which overrides the configuration of your php.ini file. It basically turns on display_errors flag used to display PHP errors. The second line sets display_startup_errors flag to enable display of startup errors.

The third line specifies what kind of error messages you want to be reported. We use E_ALL to report all kinds of errors.

Here are the different types of errors that PHP is capable of reporting.

  • E_ERROR : Fatal runtime error. Execution of script has been halted
  • E_WARNING : Nonfatal runtime error. Execution of script has been halted
  • E_PARSE : Compile time error generated by the parser
  • E_NOTICE : Script found something that may be an error. It is just a warning.
  • E_CORE_ERROR :When Fatal errors occur during the initial startup of script
  • E_CORE_WARNING :When Nonfatal errors occur during the initial startup of script
  • E_ALL : Includes All errors and warning

So if you want to report only fatal error, update the third line above to

error_reporting(E_ERROR);

Sometimes, even after the above configuration, PHP may not dispaly all errors such as missing semicolons, etc. Therefore, you need to open php.ini file and add the following line to it.

display_errors = on

This will help you identify even syntax errors and parse errors, that are not displayed using ini_set function.

You can find the location of PHP.ini file in php_info() function. Its value is present against “Loaded configuration file” variable.

In this article, we have learn’t how to enable error reporting in PHP. We have also learnt the different kinds of error reporting supported in PHP. However, please note, it is advisable to enable error reporting only in development environment and not production environment. Otherwise, your website visitors & users will be able to see all error messages.

Also read:

How to Redirect URL/Page to Particular Section with Anchor Link
How to Convert Docx to PDF using Python
How to Setup Passwordless SSH Login
How to Change NGINX User
How to Remove URL parameters using .htaccess

Leave a Reply

Your email address will not be published. Required fields are marked *