PHP programming (PHP: Hypertext Preprocessor) is a reflective programming language originally designed for producing dynamic Web pages. PHP is mainly used in server-side application software.
Every program no matter how well written can have bugs or other defects - there’s no exception to this rule, including in your PHP scripts or any other software program that you develop. Debugging is the process of finding the bugs so they can be fixed. There are so many debugger eliminating tools have been developed by the PHP programmer, choose the best tool for helping you find the bugs fast and eliminate them from your PHP programs
There are many techniques used for PHP debugging, varying from desperate PHP code inspection to angry keyboard slamming. A less extreme and more useful approach would be to use print statements and logging embedded in your PHP development code; printing out variable values throughout various stages of the program execution.PHP does not have an internal debugging facility. You can use one of the external debuggers though.
Explore various methods for debugging PHP programming, including turning on error reporting in Apache and PHP, and by placing strategic print statements to locate the source of more difficult bugs through a simple example PHP script. The PHP eclipse plug-in for Eclipse, a slick development environment with real-time syntax parsing abilities, will also be covered, as well as the DBG debugger extension for PHPeclipse.
There are many PHP debugging techniques that can save you countless hours when coding. An effective but basic debugging technique is to simply turn on error reporting. Another slightly more advanced technique involves using print statements, which can help pinpoint more elusive bugs by displaying what is actually going onto the screen. PHPeclipse is an Eclipse plug-in that can highlight common syntax errors and can be used in conjunction with a debugger to set breakpoints.
Setting up
To learn the concepts described in this article, you are going to need PHP, a Web server, and Eclipse. The latest version of PHP supported by the debugger extension is V5.0.3.
We need a Web server to parse the pages you create in PHP and display them to the browser. This article uses Apache V2. However, any Web server will suffice.
To take advantage of some of the debugging techniques 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 also need to download that.
You also need the debugger module extension for PHP. Installing it is a bit tricky. Carefully follow the instructions for installing the debugger extension. For now, comment out the lines where you are asked to load and configure the extension in PHP in the php.ini file. We’ll uncomment those lines when we’re ready to use the debugger.
Error messages
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. Later, you will see that the PHPeclipse plug-in aides significantly in the development and debugging process by underlining syntax errors on the fly and by marking syntax errors with a red "x" when saving your file.
Let’s take a look at how to turn error reporting on in the php.ini file and set the level of error reporting. Then you’ll learn how to override these settings in the Apache configuration file.
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, as shown in the instructions in the Install PHP and Apache V2 on Linux document (see Resources). There are a couple configuration variables you should know about when debugging your PHP applications. Here they are with their default values:
display_errors = Off
error_reporting = E_ALL
You can discover the current default values of these variables by searching for them in the php.ini file. The purpose of the display_errors 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 Off:
display_errors = On
The error_reporting 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
Restart Apache, and you’re all set. Next, you’ll learn how to do the same thing on Apache.
Error reporting in the 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 <apache2-install-dir>/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.
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 in Listing 1.
Listing 1. A simple PHP that generates an error
<?php
print("The next line generates an error.<br>");
printaline("PLEASE?");
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.
Debugging using print statements
In my years as a programmer where I developed applications on Linux® boxes with no handy GUI to tell me where my bugs were, I quickly caught on that the more print statements I laced my program with the more chances I had of narrowing down the bugs in my application to a single line. Create another PHP file, test2.php, and define it as shown in Listing 2.
Listing 2. Display all variables submitted via GET
<?php
$j = "";
print("Lets retrieve all the variables submitted to this ");
print("script via a GET request:<br>");
foreach($_GET as $key => $i){
print("$key=$j<br>");
}
if($_GET[’Submit’] == "Send GET Request")
$j = "done!<br>";
?>
<form method="GET">
Name: <input name="name"><br>
Email: <input name="email" size="25"><br>
<input name="Submit" type="submit" value="Send GET Request">
</form>
Using PHPeclipse:
PHPEclipse is a set of plugins for the Eclipse Framework which provide and integrated IDE for PHP programmer and developers. Eclipse, and PHPEclipse are written in Java, and will run on all graphical desktop environments. PHPEclipse is Open Source software, freely available under the Common Public Licence.
The PHPeclipse plug-in for Eclipse is a popular tool used for developing PHP applications. Start Eclipse and specify your workspace directory as the www directory for Apache (c:\www on my machine). Now click on File > New > Project. The New Project wizard will pop up. Double-click on the PHP folder and select PHP Project. Click Next, enter a project name, debugArticle, and click Finish.
If you set up your Web server to listen to port 80, you don’t need to change anything. Otherwise, go to the Navigator window and right-click on your PHP project, debugArticle. Select Properties, then click PHP Project Settings. Click Configure Workspace Settings and change localhost appropriately or add the port your Web server is listening on (http://localhost:8080, for example). Click Apply and you’re set.
The Navigator Window should display your project and a single .project file. Right-click on your project as you did before, except this time select New > PHP File. Replace *.php with the name of the PHP file you want to create, test3.php, and click Finish. A new file should appear in the Eclipse IDE. You may have to navigate the bottom window to the PHP Browser to view the current output of your PHP file.
