Displaying Errors and Error Reporting in PHP

Hi, in this post I will be talking about displaying errors and error reporting in PHP.

An issue you may encounter when it comes to PHP debugging is not being able to see the errors that occur. No, you will not want to enable displaying/reporting of errors on the live site since this may expose sensitive data which could cause security problems in your site but you will definitely want to see the errors when they occur on your development machine.

Displaying Errors:

There are 2 ways to enable displaying errors in PHP:

1-  Turn on display_errors in your PHP server. Which will make error display possible for all your PHP scripts.
2- Turn on display_errors for an individual script.

I am using WAMP Server on my development machine and here is how you can enable display_errors on WAMP Server:

1- Click on the WAMP Server icon
2- Go to “PHP” -> “PHP Settings” then click “display_errors”

wampserver-display-errors

Note: You could also edit this setting in “php.ini” configuration file.

To turn on display_errors for a single script all you need to do is add the line below as the first line of your PHP script:

This line tells your PHP server that you want to see any errors that occur while executing your PHP script.

Error Reporting:

There are 2 ways to enable error reporting in PHP:

1- Enable error reporting in “php.ini”.
2- Enable error reporting for an individual script.

Here is how you can enable error reporting in WAMP Server:

1- Click the WAMP Server icon
2- Go to PHP and click “php.ini” to open the configuration file

wampserver-php.ini

Ctrl+F for “error_reporting” and edit the line as below to display all the errors (if not already set to display them (E_ALL)) and save the file.

wampserver-php.ini-error-reporting

To enable error reporting for an individual PHP script all you need to do is add “error_reporting (E_ALL)” line to the beginning of your script as below:

Now you should be able to see all types of errors that may occur during the execution of your PHP script. You can change “E_ALL” with other possible error types to restrict the reporting to a specific type (or multiple types).

Check this page for a list of PHP error types.

Hope this helps.
Good luck,
Serdar

Leave a Reply

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