Surprising facts of error reporting in PHP

error reporting in PHP

Error reporting is one of the most important and crucial parts of any software when it comes to development. It helps developers to analyze why software is failing and how someone can improve its code.

When it comes to PHP, it is a scripting language and all the errors are reported on run time and the pain for the developer starts when he is unable to see any errors and the application dies silently. Now the problem is how to resolve it and how to do error reporting in PHP, Well for the basics, below are 3 lines of code that can show you all the errors:

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

The above lines of code will turn on all the errors and warnings in your application.

ini_set is a PHP function that will override your configuration found inside the php.ini file. In the above lines of code ini_set(‘display_errors’, 1); display all the errors to the user. display_errors is just a directive. The same is for display_startup_errors, both are just directives that are found in the PHP.ini file. You can find more details in their official documentation. There are some errors that will not be displayed which these two directive and those are missing semicolons and missing curly braces, To show those errors, you have to modify your PHP.ini file

Edit PHP.ini for Error reporting:

Additionally, you can configure and modify PHP.ini file to show errors, make sure you have that environment only for testing and not for production.

display_errors = on

the above directive will turn on all the errors for example syntax error, parse error ad these errors can only be shown through PHP.ini display_errors = on.

This directive must be turned off in production otherwise it can create some serious problems for you.

Different levels of Error reporting in PHP:

There are different levels of error reporting in PHP which can cause due to various reasons. Let’s discuss some Error types

  1. Warnings
  2. Notice
  3. Parse Error
  4. Fatal Errors

Warnings happen if the variable is undefined or a file you included is not there or for some similar reasons, does it break your application? NO, it does not break our application.

Notice are minor errors, they are somewhat similar to warning errors because due to notices our code execution does not stop and the application is not broken, Notice happens if something is going to get obsolete in a future version of code or we are using something obsolete or you are calling a variable which is not defined in anywhere.

Parse error happens if you are missing some semi-colon or some brackets or some kind of misspelling. execution of cod will be stoped and you will see the error, figuring it out could be tricky for new developers.

Fatal errors are the most critical errors in your application, they will stop the execution of all code and you cannot go around without fixing it. it happens due to some missing function or calls to undefined functions, the example given below:

function fatal_func()
 echo "Show fatal errors";

// i am not calling the fatal_func() just to show you how fatal errors work


Error reporting with .htaccess file:

As a developer, the most common access to any server level file is through .htaccess and you can modify the .htaccess file to display errors:

php_flag display_startup_errors on
php_flag display_errors on

Just like we added some display_errors and display_startup_errors inside our PHP code, .htaccess also have the same directives, which will show all the errors in your application.

Turn off all errors:

occasionally you can turn off all your error reporting and usually it will happen if you are going in production mode and the code is very simple for that:

error_reporting(0); // it will turn off all the errors from your php application

// you can report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors

// Report all PHP errors

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

Hope that clears your some confusion regarding error reporting in PHP.