Home > Article > Backend Development > Debugging techniques for PHP programmers_PHP tutorial
Introduction
There are many PHP debugging techniques that can save a lot of time when coding. An effective yet basic debugging technique is to turn on error reporting. Another, slightly more advanced technique involves using print statements, which can help pinpoint harder-to-find bugs by displaying what actually appears on the screen. PHPEclipse is an Eclipse plug-in that highlights common syntax errors and can be used in conjunction with the debugger to set breakpoints.
To learn the concepts described in this article, PHP, a web server, and Eclipse are required. The debugger extension supports PHP version V5.0.3.
We need a web server to parse pages created with PHP and display them to the browser. Apache2 is used in this article. However, any web server will suffice.
To take advantage of some of the debugging techniques described in this article, you need to install Eclipse V3.1.1 and the plug-in PHPEclipse V1.1.8. Since Eclipse requires Java™ technology, download it as well.
Also requires the PHP debugger extension module. Installing it is a little tricky. Please follow the instructions for installing the debugger extension carefully. Now, comment out the lines in the php.ini file that require the PHP extension to be loaded and configured. Uncomment it when you need to use the debugger.
Error message
Error messages are your first line of defense as a developer. No one wants to develop code in PHP on a server that is not configured to display error messages. However, please remember that when you have debugged your code and are ready to run it, you should make sure that error reporting is turned off as you do not want your site visitors to see error messages as this will give them enough information to exploit the site's weaknesses and Hack the site.
Error messages can also serve themselves well, as they show the correct line of code that threw or generated the error. This way, debugging becomes a matter of looking in the browser at the line number where the generated error appears, and inspecting that line in the code. Later, you'll see that the PHPEclipse plug-in can be of great assistance during development and debugging by underlining syntax errors on the fly and marking syntax errors with a red "x" when saving the file.
Let’s first look at how to enable error reporting and set the level of error reporting in the php.ini file. You'll then learn how to override these settings in Apache's configuration files.
PHP Error Report
There are many configuration settings in the php.ini file. You should have set up your php.ini file and placed it in the appropriate directory, as documented in the instructions for installing PHP and Apache 2 on Linux (see Resources). There are two configuration variables that you should be aware of when debugging PHP applications. Here are the two variables and their default values:
display_errors = Off
error_reporting = E_ALL
The current default values of these two variables can be found by searching for them in the php.ini file. The purpose of the display_errors variable is obvious - it tells PHP whether to display errors. The default value is Off. However, to make the development process easier, set this value to On:
display_errors = On
The default value of the error_reporting variable is E_ALL. This setting will show everything from bad coding practices to harmless tips to errors. E_ALL is a bit too granular for development purposes, as it also displays hints on the screen for small things (such as variables not being initialized), which messes up the browser's output. I only want to see errors and bad coding practices, but not harmless tips. So, please replace the default value of error_reporting with the following value:
error_reporting = E_ALL & ~E_NOTICE
Restart Apache and you're all set. Next, you'll learn how to do the same thing on Apache.
Error reporting on server
Depending on what Apache is doing, turning on error reporting in PHP may not work since there may be multiple versions of PHP on the machine. Sometimes it's difficult to tell which PHP version Apache is using 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 issue. However, there is a way to configure PHP variables in Apache so that the correct error level is set.
Also, it's a good idea to know how to set these configuration variables on the server side to override or preempt the php.ini file, thus providing a higher level of security.
When configuring Apache, you should have already been exposed to the basic configuration in the http.conf file in
To do what you have already done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini files:
php_flag display_errors on
php_value error_reporting 2039
This overrides the flag already set in the php.ini file for display_errors, as well as the value of error_reporting. The value 2039 represents E_ALL & ~E_NOTICE. If you prefer to use E_ALL, set the value to 2047. Again, you still need to restart Apache.
Next, test error reporting on the server.
Test bug report
If error reporting is enabled, it will save a lot of time. Errors in PHP point to errors in your code. Create a simple PHP file test.php and define it as shown in Listing 1.
Listing 1. A simple PHP that generates errors
print("The next line generates an error.
");
printaline("PLEASE?");
print("This will not be displayed due to the above error.");
?>
The first print() statement displays its contents to the web browser. But the second statement generates an error and displays it on the web page. This causes the last print() statement to not work, as shown in Figure 1.
Bug reporting is now enabled! Next, use print statements to help debug your application.