Although we normally develop in local server with error reporting enabled By default, once the project is uploaded to an online server, the error sampling disappears. In these cases, and in those in which we want to control the reporting of errors in localhost, we must know how to show or hide errors in PHP.
In this tutorial I am going to show you the different ways of show and hide php code errors. From the simplest with a few lines of code to making certain changes to the web server configuration.
The most important concepts that I deal with in this tutorial are:
- How to turn on error notification in the browser.
- As show all types of php errors.
- Disable notification of all types of errors.
- Show type errors a concrete type or class.
- Trigger errors from the server.
- Steps to print the errors on the screen
- How to enable error display in PHP
- 1. Show all errors in PHP
- 2. Show NOTICE type errors.
- Show all errors in PHP
- Disable notification of all types of errors
- show errors of specific types
Steps to print the errors on the screen
The steps to show errors occurred in php there are basically two:
- Enable the display of errors on the screen. This can be done in different ways but in this tutorial we will focus on doing it from code.
- indicate that types of errors you want to display. This means that we will indicate which, of all the possible types of errors, we want to be shown in the browser.
How to enable error display in PHP
Before we begin to explain the different types of errors that we can treat, we are going to see how to activate the display of PHP errors by the browser. We will achieve this with the function ini_set and the variable of setting display_errors.
Once the directive is established display_errors to 1 we can begin to configure and visualize the types of errors that we want during our debugging.
Next we will indicate that bug monster level we want to activate, we have several options (levels) with which to configure the system. All of this configuration is done through PHP’s error_reporting function.
In PHP 5 there are 16 different error levels, each identified with a different integer value. Do not panic! I will export below only the most important ones.
Here I show you the most important types of errors and how to show them.
1. Show all errors in PHP
That is to say, all the errors of all the levels with the exception of one, the errors of strict type.
Although it may seem crazy, if our programming is more or less correct we will find a low number of errors despite showing all of them.
//primera forma de hacerlo error_reporting(E_ALL); //segunda forma de activar el mostrado de todos los errores error_reporting(-1);
2. Show NOTICE type errors.
NOTICE type errors are errors that are not fatal but that it would be interesting to solve for the application to function correctly.
A typical error of this type is when we use a variable that we have not defined in advance, the famous NOTICE: undefined index or NOTICE: undefined variable
Show all errors in PHP
This is the snippets The most general of all, which has the simple objective of showing all the errors in our PHP code, without limitations:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
Disable notification of all types of errors
ini_set('display_errors', 0); error_reporting(0);
show errors of specific types
If what we need is only to display errors of a specific type or category, we can use the errorr_reporting command, indicating the type of error by its code.
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_STRICT);
The full list of error types is as follows:
1 E_ERROR 2 E_WARNING 4 E_PARSE 8 E_NOTICE 16 E_CORE_ERROR 32 E_CORE_WARNING 64 E_COMPILE_ERROR 128 E_COMPILE_WARNING 256 E_USER_ERROR 512 E_USER_WARNING 1024 E_USER_NOTICE 6143 E_ALL 2048 E_STRICT 4096 E_RECOVERABLE_ERROR