Error Reporting in PHP

Error Reporting in PHP

Generally, PHP developers want to see the PHP exceptions and error while developing of any PHP project for better and appropriate functioning of a project.

However, they hide those exceptions and errors while delivering that project to the client. The developers enable only the custom exceptions for maintaining the flow of the web application.

Example: Suppose we have created an application to add two integers.

 

 

RUN:

error reporting in PHP

error reporting in PHP

 

Kindly notice, In above example there are two errors are generating for the variables $num1 and $num2 at line 2 & 3.

These are because at the initial moment the declared variables $num1 and $num2 does not hold any value.

However, from the client and end user point of view, this error should not come. So now the solution is to be use function error_reporting().

This function takes the argument 1 or 0. The function with the argument 1 like:

error_reporting(1);

This argument will enable all the errors on the web page.

error_reporting(0);

This argument will disable all the error from the web page and they will hide.

Now if we run the same above code again with function error_reporting(0)

error reporting in PHP

error reporting in PHP

The exactly same operation we can do with the php.ini file with the following steps.

  1. Go to your php.ini file.
  2. Open that file in your notepad.
  3. Search for error_reporting and hit enter.
  4. If the error reporting is turned off then you will see the value 0.
  5. Change that value to E_ALL
  6. There are many other values you can change but for now, save the php.ini file and restart your server. Now the changes will effect.