This article describes various ways to debug a PHP application, including opening error reports in Apache and PHP, and finding the source of more difficult bugs by placing a strategic print statement in a simple PHP script. It also introduces the PHPEclipse plug-in for Eclipse, a flexible development environment with real-time parsing capabilities and an introduction to PHPEclipse's DBG debugger extensions.
Introduction
There are many PHP debugging techniques that can save a lot of time when coding. An effective but basic debugging technique is to turn on error reporting. Another slightly more advanced technique, including the use of print statements, is to help pinpoint bugs that are more difficult to spot by displaying what actually appears on the screen. PHPEclipse is an Eclipse plugin that emphasizes common syntax errors that can be combined with the debugger for setting breakpoints.
Set
To learn the concepts described in this article, PHP, WEB servers, and Eclipse are required. The PHP version supported by the debugger extension is V5.0.3.
We need a WEB server to parse the pages created in PHP and display them to the browser. The Apache2 is used in this article. However, any WEB server can meet the requirements.
To take advantage of some of the debugging techniques described in this article, you need to install Eclipse V3.1.1 and plug-in PHPEclipse V1.1.8. Since Eclipse requires Java? Technology, so also download it.
PHP's Debugger Extension module is also required. It's a little bit cumbersome to install. Follow the instructions for installing the debugger extensions carefully. Now, comment out the lines in the php.ini file that require the load and configuration of the PHP extension. When you need to use the debugger, uncomment it again.
See Resources for download information. The error message is now described.
error Message
The error message is the first line of defense as a developer. Nobody wants to develop code in PHP on a server that is not configured to display error messages. However, keep in mind that when the code is debugged and ready to run, you should make sure that you close the error report because you don't want the site's visitors to see the error message because it gives them enough information to take advantage of the site's weaknesses and black out the site.
You can also serve yourself with error messages because they display the correct line of code that throws or generates an error. In this way, debugging becomes the line number displayed in the browser to view the generated error, and the line is checked in the code. Later, you'll see that the PHPEclipse plugin provides great help during development and debugging by instantly underlining syntax errors and labeling syntax errors with red "X" when saving a file.
Let's start by looking at how to turn on Error reporting in the php.ini file and set the level of error reporting. You will then learn how to override these settings in the Apache configuration file.
Error Reporting for PHP
There are many configuration settings in the php.ini file. You should have your own php.ini file set up and put it in the appropriate directory, as shown in the documentation for installing PHP and Apache 2 on Linux (see Resources). When debugging a PHP application, you should know two configuration variables. The following are the two variables and their default values:
Display_errors = Off error_reporting = E_all |
By searching for them in the php.ini file, you can find the current default values for both variables. The purpose of the display_errors variable is obvious-it tells PHP if it shows an error. The default value is OFF. However, to make the development process easier, please set this value to on:
The default value for the error_reporting variable is e_all. This setting shows all the information from bad coding practices to harmless prompts to errors. E_all is a bit too thin for the development process because it shows hints on the screen for small things (such as uninitialized variables), which can mess up the browser's output. I just want to see errors and bad coding practices, but don't want to see harmless hints. Therefore, replace the default value of error_reporting with the following values:
error_reporting = E_all & ~e_notice |
Restart Apache and it's all set up. Next, you'll learn how to do the same thing on Apache.
Error Reporting on the server
Depending on what Apache is doing, opening an error report in PHP may not work because there may be multiple versions of PHP on your computer. It is sometimes difficult to tell which PHP version Apache is using because Apache can only view one php.ini file. It is a security issue to not know which php.ini file Apache is using to configure itself. However, there is a way to configure PHP variables in Apache to ensure that the correct level of error is set.
Also, it is best to know how to set these configuration variables on the server side to veto or preempt php.ini files, providing a higher level of security.
When you configure Apache, you should have been exposed to the basic configuration in the http.conf file in/conf/httpd.conf.
To do what has already been 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 flags already set for display_errors in the php.ini file, as well as the value of error_reporting. A value of 2039 represents E_all & ~e_notice. If you prefer to use E_all, set the value to 2047. Again, you'll restart Apache.
Next, you will test the error report on the server.
Test Error Report
If you start an error report, you save a lot of time. Errors in PHP can point to errors in your code. Please create a simple PHP file, test.php, and define it as shown in Listing 1.
Listing 1. A simple PHP that generates a bug
"); Printaline ("please?"); Print ("This is not being displayed due to the above error."); ? > |
The first print () statement displays its contents to a Web browser. However, the second statement generates an error and is displayed on the Web page. This causes the last print () statement to have no effect, as shown in 1.
Figure 1: Build Error
The error report is now open! Next, use the print statement to help debug your application.
introducing the PRINT statement
Because a functional bug in an application does not produce an error, in all debugging strategies, knowledge about how to properly place and use print or Die statements to debug a PHP application is a good asset. You can use the print statement to narrow down the problem statements in your code, which are syntactically error-free and not a bug, but from the functionality of the code. These are the hardest bugs to find and debug because they don't throw errors. The only thing you know is that the content displayed on the browser is not what you want, or what you want to save in the database is not saved at all.
Suppose you are processing form data sent over a GET request and want to display information to the browser, but for some reason the data is not committed correctly, or it cannot be read correctly from the GET request. To debug such problems, it is important to use the print () or Die () statement to know what the value of the variable is.
The die () statement aborts program execution and displays text on the Web browser. The die () statement is particularly useful if you do not want to comment out the code and want to display only the information and error messages that precede the error and do not want to display the following information.
Let's test the concept in PHP with the print statement
debugging with the print statement
When I was a programmer, when I was on Linux? When developing the application, there is no convenient GUI to tell me where the bug is, and I quickly find that the more print statements I put in the program, the greater the chance that I can narrow the bug down to one line in the application. Please create another PHP file, test2.php, and define it as shown in Listing 2.
Listing 2. Show all variables submitted via GET
http://www.bkjia.com/phpjc/508474.html www.bkjia.com True http://www.bkjia.com/phpjc/508474.html techarticle This article describes the various ways to debug a PHP application, Including opening error reports in Apache and PHP, and by placing a strategic print statement in a simple PHP script, find ... |