This article covers various ways to debug PHP applications, including turning on error reporting in Apache and PHP, as well as finding the source of more difficult bugs by strategically placing print statements in a simple PHP script. You'll also learn about the PHPEclipse plug-in for Eclipse, a flexible development environment with real-time syntax parsing capabilities, and the DBG debugger extension for PHPEclipse.
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.
Settings
To learn the concepts described in this article, you will need PHP, a web server, and Eclipse. 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 introduced in this article, you need to install Eclipse V3.1.1 and the plug-in PHPEclipse V1.1.8. Since Eclipse requires Java® technology, you'll also need to download it.
You also need 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.
See Resources for download information. Now about the error messages.
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 be used to your advantage, as they will 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 Copy after login |
display_errors = On Copy after login |
error_reporting = E_ALL & ~E_NOTICE Copy after login |
php_flag display_errors on php_value error_reporting 2039 Copy after login |
<?php print("The next line generates an error.<br>"); printaline("PLEASE?"); print("This will not be displayed due to the above error."); ?> Copy after login |
<?php $j = ""; print("Lets retrieve all the variables submitted to this "); print("script v </p> <p align="left"></p><div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/508474.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/508474.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">This article describes various methods of debugging PHP applications, including opening error reports in Apache and PHP, and by A simple PHP script to place strategic print statements, find...</span> </div> <div class="art_confoot"></div> Copy after login |