PHP Debugging Techniques – Part I
There are many PHP debugging techniques that can save you countless hours when coding as error reporting, using print statements etc. Let’s go through a little detail of these techniques.
Error messages are your first line of defense as a developer. You don’t want to be developing code in PHP on a server that is not configured to display error messages. However, keep in mind that when your code is debugged and ready to go live, you want to make sure error reporting is turned off because you don’t want visitors to your site seeing error messages that may give them enough knowledge to exploit a weakness and hack your site.
You can also use error messages to your advantage because they display the exact line of code that threw or generated an error. This makes debugging a matter of looking at the line number shown on the browser by the generated error and checking that line number in your code.
1. Error Reporting
An effective but basic debugging technique is to simply turn on error reporting.
Error Reporting in PHP:
There are many configuration settings in the php.ini file. You should already have set up up your php.ini file and placed it in the appropriate directory. There are a couple configuration variables you should know about when debugging your PHP applications. Here they are with their default values (You can discover the current default values of these variables by searching for them in the php.ini file.) :
display_errors = Off
error_reporting = E_ALL
display_errors: The purpose of this variable is self-evident — it tells PHP whether or not to display errors.The default value is
Off. To make your life easier in the development process, however, set this value to
On by replacing
display_errors = On
error_reporting This variable has a default value of E_ALL. This displays everything from bad coding practices to harmless notices to errors. E_ALL is a little too picky for my liking in the development process because it clutters the browser output by displaying notices on the screen for small things like uninitialized variables. I prefer to see the errors, any bad coding practices, but not the harmless notices.Therefore, replace the default value of
error_reporting as follows:
error_reporting = E_ALL & ~E_NOTICE
Why Enable E_NOTICE?
• Warns about un-initialized variables.
• Various non-critical issues that could potentially cause problems.
Error Reporting in Server:
Depending on what Apache is doing, turning error reporting on in PHP may not work because you may have multiple PHP versions on your computer. It’s sometimes hard to tell which PHP version Apache is pointing to because Apache can only look at one php.ini file. Not knowing which php.ini file Apache is using to configure itself is a security problem. However, there is a way to configure PHP variables in Apache to guarantee the setting of the correct error levels.
Also, it’s good to know how to set these configuration variables on the server side to veto or pre-empt the php.ini file, providing a greater level of security.
You should already have toyed with basic configurations in the http.conf file at /conf/httpd.conf when you configured Apache.
To do the same as you just did in the php.ini file, add the following lines to your httpd.conf to override any and all php.ini files:
php_flag display_errors on
php_value error_reporting 2039
This overrides the flag you have set for display_errors in the php.ini file, as well as the value of error_reporting. The value 2039 stands for E_ALL & ~E_NOTICE. If you prefer E_ALL, set the value to 2047, instead. Again, make sure you restart Apache.
As a Summary:
# Inside PHP.ini
error_reporting = E_ALL
# Inside httpd.conf or .htacess
php_value error_reporting 2047
# Inside a script
ini_set(“error_reporting”, E_ALL | E_STRICT);
/* or */
error_reporting(E_ALL | E_STRICT);
Next, we’ll test error reporting on your server.
Testing Error Reporting
You will save a great deal of time if you leave error reporting enabled. Errors in PHP point you right to the error in your code. Create a simple PHP file, test.php, and define it as shown below.
print(“The next line generates an error.”);
print(“This will not be displayed due to the above error.”);
The first print() statement should display its contents to the Web browser. However, the second statement generates and displays an error to the Web page. This results in the last print() statement do nothing, as shown in Figure 1
Figure 1 : Generating an Error