Basic PHP Error Reporting Techniques
Every program written is susceptible to bugs and errors. No programming language is bug-proof or error-proof. However, the occurrence of these bugs and errors can be cached, error-trapped and de-bugged.
Following are basic techniques on how to display PHP error messages:
- The importance of error messages
Error messages are very convenient for debugging as they display the exact line of code that’s causing the error.
Note: Make sure to disable error reporting when you’re site’s about to go live. You don’t want visitors to see the backend error. These error messages expose the vulnerable part of your site which may be used by malicious visitors as an entry point to hack and exploit your website.
- Turning on error reporting in PHP
The php.ini file is set default configuration values.
display_errors = Off
Error_reporting = E_ALL
The display_errors variable tells PHP whether or not to display errors. Simply change the value to “On” when developing a PHP program. Error_reporting with an “E_ALL” value tells the server to display everything – from harmless warnings to bad coding practices, to actual coding errors.
If you only want to display the “bad coding practices” warnings and the actual errors, but not those harmless coding notices, you can set Error_reporting value to E_ALL & ~E_NOTICE. Ideally the error reporting settings should be:
display_errors = ON
error_reporting = E_ALL & ~E_NOTICE
For the changes to take effect, restart APACHE.
- Enabling server-level error reporting
There are times when turning on error reporting in PHP may not work as you may have multiple versions of PHP installed in your computer. It is possible that APACHE is not recognizing the recently configured php.ini file as it is already pointing to one of the php.ini files in your computer. To avoid glitches like this, you can set the error reporting variables on server-level.
Configuring error reporting on the APACHE server requires that you add the line below to your http.conf file:
php_flag display_error on
php_value error_reporting 2039
The above values override the settings you’ve set on your php.ini file. The 2039 value is equivalent to E_ALL & ~E_NOTICE. If you prefer to have the E_ALL value, set it to 2047 instead.
- Testing error reporting
Turning error reporting on will save you a great deal of time. PHP error messages will point you directly to the error in your source code. To test your error reporting configuration, create a simple test.php file and define it as shown below:
print(“The line below will generate an error.<br>”);
printaline(“This is the line that will generate an error.”);
print(“This line will not be displayed due to the error caused by the above line.”);
The first print() function will be displayed into the web browser. However, the second statement will generate an error message and will cause the third statement not to be executed and displayed.
Turning on PHP error reporting is just the start. As a developer, this serves as your first line of defense.