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.
See Related topics for download information. Now let's move on to error messages.
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 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:
Introducing print statements
Because functional bugs in your application don't generate errors, knowledge on how to accurately place and use print
or die
statements to debug your PHP application can be a great asset in your arsenal of debugging strategies. You can use print
statements to narrow down the locations of problem statements in your code that may not be syntactically incorrect or bugs in the code, but they are bugs in the functionality of your code. These are the hardest bugs to find and debug because they throw no errors. You only know that what is being displayed to the browser isn't what you intended, or that what you thought was being stored in your database isn't being stored at all.
Suppose you are processing form data sent in via a GET
request and want to display the information to the browser, but for whatever reason, the data is either not being submitted properly, or it isn't being read from the GET
request properly. To debug such a problem, it's important to know what the value of the variable is, using a print()
or a die()
statement.
The die()
statement halts program execution and displays text to the Web browser. The die()
statement is particularly useful if you don't want to have to comment out your code, and you only want everything up to the error and the error displayed and nothing after.
Let's test this concept of using print statements in PHP.
print
or die
statements to debug your PHP application can be a great asset in your arsenal of debugging strategies. You can use print
statements to narrow down the locations of problem statements in your code that may not be syntactically incorrect or bugs in the code, but they are bugs in the functionality of your code. These are the hardest bugs to find and debug because they throw no errors. You only know that what is being displayed to the browser isn't what you intended, or that what you thought was being stored in your database isn't being stored at all.GET
request and want to display the information to the browser, but for whatever reason, the data is either not being submitted properly, or it isn't being read from the GET
request properly. To debug such a problem, it's important to know what the value of the variable is, using a print()
or a die()
statement.die()
statement halts program execution and displays text to the Web browser. The die()
statement is particularly useful if you don't want to have to comment out your code, and you only want everything up to the error and the error displayed and nothing after.