As we all know, code debugging is very important during the development process. debugging techniques for common errors can help improve the efficiency of code writing. This article describes various methods for debugging PHP applications, including opening error reports in Apache and PHP, and placing strategic print statements in a simple PHP script, find the source of a more difficult bug. The PHPEclipse plug-in for Eclipse is also introduced. this is a flexible development environment with real-time syntax parsing capabilities. The DBG debugger extension for PHPEclipse is also introduced.
Introduction
Many PHP debugging technologies can save a lot of time in coding. An effective but basic debugging technique is to open an error report. Another slightly more advanced technique involves the use of print statements to help precisely identify more difficult bugs by actually appearing on the screen. PHPEclipse is an Eclipse plug-in that can emphasize common syntax errors and can be used with the debugger to set breakpoints.
Set
To learn the concepts described in this article, PHP, Web server, 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 with PHP and display them to the browser. Apache2 is used in this article. However, any Web server can meet the requirements.
To use some debugging techniques described in this article, you need to install Eclipse V3.1.1 and PHPEclipse V1.1.8. Because Eclipse requires Java™Technology, so you need to download it.
You also need the PHP debugger extension module. Installing it is a little troublesome. Follow the instructions for installing the debugger extension carefully. Now, comment out the lines that require loading and configuring php extensions in the PHP. ini file. Cancel the comment when you need to use the debugger.
See references for download information. This section describes error messages.
Error message
Error messages are the first line of defense for developers. No one wants to develop code with PHP on a server that is not configured to display error messages. However, remember that when the code debugging is complete and you are ready to run it, make sure that the error report is disabled because you do not want the site visitors to see the error message, this will give them enough information to take advantage of the site's vulnerabilities and hack the site.
You can also use error messages to serve yourself, because they will display the correct code lines that throw or generate errors. In this way, debugging becomes to view the row number of the generated error in the browser, and check this line in the code. Later, you will see the PHPEclipse plug-in instantly underline the syntax error and mark the syntax error with the red "x" when saving the file, it can be of great help in the development and debugging process.
First, let's take a look at how to enable the error report in the php. ini file and set the error report level. Then we will learn how to overwrite these settings in the Apache configuration file.
PHP error report
The php. ini file contains many configuration settings. You should have set your php. ini file and place it in the appropriate directory, as shown in instructions for installing PHP and Apache 2 on Linux (see references ). When debugging a PHP application, you should know two configuration variables. The two variables and their default values are as follows:
The code is as follows:
Display_errors = Off
Error_reporting = E_ALL
By searching for these variables in the php. ini file, you can find the current default values of these two variables. The purpose of the display_errors variable is obvious-it tells PHP whether an error is displayed. The default value is Off. However, to make the development process easier, set this value to On:
The code is as follows:
Display_errors = On
The default value of error_reporting variable is E_ALL. This setting displays all information from poor coding practices to harmless prompts to errors. E_ALL is a little too detailed for the development process, because it displays a prompt on the screen for some trivial matters (for example, the variable is not initialized), it will mess up the browser output. I only want to see errors and bad code practices, but do not want to see harmless prompts. Therefore, replace the default value of error_reporting with the following values:
The code is as follows:
Error_reporting = E_ALL &~ E_NOTICE
Restart Apache and set all the settings. Next, we will learn how to do the same thing on Apache.
Server error report
Depending on what Apache is doing, opening an error report in PHP may not work, because there may be multiple PHP versions on the computer. Sometimes it is difficult to tell which PHP version Apache is using, because Apache can only view one php. ini file. I don't know which php. ini file Apache is using to configure itself as a security issue. However, there is a way to configure the PHP variable in Apache to ensure that the correct error level is set.
In addition, it is better to know how to set these configuration variables on the server side to reject or preemptible the php. ini file to provide higher-level security.
When configuring Apache, you must have been exposed to the basic configuration in the http. conf file in/conf/httpd. conf.
To add the following lines to the httpd. conf file to overwrite any php. ini file:
The code is as follows:
Php_flag display_errors on
Php_value error_reporting 2039
This overwrites the flag set for display_errors and the value of error_reporting in the php. ini file. The value 2039 represents E_ALL &~ E_NOTICE. If you want to use E_ALL, set the value to 2047. Similarly, restart Apache.
Next, test the error report on the server.
Test error report
If an error report is started, it will save a lot of time. Errors in PHP point to errors in the code. Create a simple PHP file test. php and define it as shown in listing 1.
Listing 1. a simple PHP with incorrect generation
The code is as follows:
Print ("The next line generates an error.
");
Printaline ("PLEASE? ");
Print ("This will not be displayed due to the above error .");
?>
The first print () statement will display its content to the Web browser. However, the second statement will generate an error and display it on the Web page. This makes the last print () statement ineffective, as shown in 1.
Figure 1. generation error
The error report is now enabled! Next, use the print statement to debug the application.
Introduction to print statements
Because functional bugs in applications do not produce errors, in all debugging policies, knowledge about how to correctly place and use print or die statements to Debug PHP applications is a good asset. You can use print statements to narrow down the problem statement positioning in the code. these statements have no syntax errors or bugs, but they are bugs in the code function. These are the most difficult bugs to discover and debug, because they do not throw errors. The only thing you know is that the content displayed on the browser is not what you want, or the content you want to save in the database is not saved at all.
Assume that you are processing the form data sent through the GET request and want to display the information to the browser. However, for some reason, the data is not submitted correctly or cannot be correctly read from the GET request. To debug this type of problem, it is important to use print () or die () statements to know the value of the variable.
The die () statement terminates program execution and displays text in a Web browser. If you do not want to comment out the code, and only want to display the information before and after the error, then the die () statement is particularly useful.
Let's use the print statement in PHP to test this concept.
Debug with the print statement
When I was a programmer®When developing applications, there is no convenient GUI to tell me where the bug is. I quickly found that the more print statements I put in the program, the larger chance is to narrow down the bug scope to a row in the application. Create another PHP file test2.php and define it as shown in listing 2.
Listing 2. show all variables submitted through GET
The code is as follows:
$ J = "";
Print ("Lets retrieve all the variables submitted to this ");
Print ("script via a GET request:
");
Foreach ($ _ GET as $ key => $ I ){
Print ("$ key = $ j
");
}
If ($ _ GET ['submit '] = "Send GET Request ")
$ J = "done!
";
?>
You may easily find bugs in listing 2! You are great! However, please note that this is a very simple script, just as an example of debugging using the print statement. This script only extracts all the variables in the GET request. if so, it will display them in the browser. A form is provided to send a variable to the server with a GET request for testing. See output, 2.
Figure 2. output of test2.php
ClickSend GET RequestButton, note that only$_GET
The requested key is displayed in the browser, but the correct value is not displayed. You can put a print statement in the loop to checkforeach
Whether data exists in each element of the loop. See listing 3.
Listing 3. using the print statement to verify the code function
The code is as follows:
...
Foreach ($ _ GET as $ key => $ I ){
Print ("Correct data? ". $ _ GET [$ key]."
");
Print ("$ key = $ j
");
}
...
The print statement is in bold. Note that the $ key value displayed on the Web browser is correct, but for some reason, the value is not correctly displayed. See the new output, as shown in 3.
Figure 3. output of test2.php after modification
Now you know that the application is correctlyGET
When the request receives a variable, it must have a bug in the code. Check the variable used to display the value.$j
Yes. Inforeach
The statement specifies$i
So it will certainly have the correct value, but it is not intended to be entered$j
. Therefore$j
Replace$i
The error is corrected quickly. after the page is Reloaded, the correct output is displayed, as shown in figure 4.
Figure 4. corrected output of test2.php
Now you can delete or comment out the print statement you just added, because a bug in the code has been found. Note that this is only a small subset of the many errors that may be encountered during application debugging. A good solution for problems that may occur when you use the database is to output SQL statements to ensure that the executed SQL statements are what you want to execute.
Now let's take a look at how to use Eclipse IDE and PHPEclipse plug-ins and debugger extensions to further help in the debugging process.
Use PHPEclipse
You may have used Eclipse, but may not be familiar with it. See references for an introduction to the Eclipse platform.
The PHPEclipse plug-in for Eclipse is a popular tool used to develop PHP applications. Start Eclipse and specify the workspace directory as the www directory of Apache (c: \ www on my machine ). ClickFile> New> Project. The New Project Wizard is displayed. Double-click the PHP folder and select the PHP Project. ClickNext, Enter the project name debugArticle, and clickFinish.
If you set the Web server to listen on port 80, no modification is required. Otherwise, go to the Navigator window, in the PHP projectDebugArticleRight-click, select Properties, and clickPHP Project Settings. ClickConfigure Workspace SettingsThen modify the appropriate localhost or add the Web server listening port (for example, http: // localhost: 8080 ). ClickApplyComplete the settings.
The Navigator window should display a project and a. project file. Right-click the project, as shown in the previous step.New> PHP File. Replace *. PHP with the name of the php file you want to create, and then clickFinish. A new file should appear in Eclipse IDE. You may need to navigate to the PHP browser at the bottom of the window to view the current output of the PHP file (see figure 5 ).
Figure 5. Eclipse PHPEclipse plug-in
Note that only Windows®Users can use the PHP browser as shown in listing 5. You can also use the same function by opening the independent browser window and directing the browser to the directory where the test script is located.
Now Let's demonstrate this application to prove its strength.
In the "use debugger" section, you will learn how to use Eclipse, PHPEclipse, and the previously downloaded debugger PHP extension to Debug PHP applications. First, learn how to use its syntax parsing function.
Syntax parsing and underline
First, check how PHPEclipse can help debug the real-time syntax parsing function of PHP applications. Depending on the actual application of this feature, first define test3.php in Eclipse, as shown below.
Note that the two characters underlined in listing 4 are underlined in Eclipse, prompting that the syntax is incorrect. PressCtrl + SSave the file and a parsing error will be displayed in Eclipse: red "x" will be added to the line corresponding to the parsing error in the code, as shown in 6.
Figure 6. Syntax error emphasis
Now we will demonstrate the PHP browser. This window provides a preview of the current PHP script, as shown in 6.
Delete the comma (,
). PressCtrl + SSave the file, observe the updates in the PHP browser window, and display Hello World (see Figure 7 ).
Figure 7. preview the PHP script in PHPEclipse
The following describes how to use a debugger to set breakpoints in PHP.
Use the debugger
With the debugger, you can set breakpoints and view the browser output before the PHP code is set to the breakpoint. Then, you can continue code execution and view the browser output before the next breakpoint, and then go to the next one until the PHP script is complete.
Now, uncomment the line commented out in php. ini in the "Settings" section and restart Apache. Now the debugger is installed, and Eclipse can be attached to it.
Now, design the debugging environment in Eclipse. Create a new test4.php file. leave it empty first. ClickRun> Debug. In the left-side pane, select php dbg Script and clickNew. Now goFileTab, enter the current projectDebugArticleAnd files to be debuggedTest4.php. Now goEnvironmentTab, and thenInterpreterSub-tab. Find the php.exe file in the PHP installation directory (my files are c: \ apps \ php5.0.3 \ php.exe ). ClickRemote DebugSub-tab, selectRemote DebugIf Windows is not used, cancel the "Open with DBGSession URL in internal browser box" check box. Set the Remote Source path to the absolute path (not the Web path) of the PHP script to be tested (my setting is c: \ www \ debugArticle \ test4.php ). ClickDebug.
Now the Debug perspective should be loaded, as shown in figure 8. Otherwise, clickWindow> Open Perspective> OtherAnd SelectDebug.
Figure 8. Debug perspective in Eclipse
Now you can set the breakpoint.
For the plug-ins and extended versions used in this article, the breakpoint function is required because PHP will buffer the output before sending it to the browser. In addition, you need not only to set a breakpoint to refresh the currently displayed data to the Web browser, so define test4.php as shown in the following figure and figure 8.
Listing 4. setting and creating breakpoints
");breakpoint();print("This won't get shown until after ");print("continuing the break-point ");breakpoint();print("END!");? |
breakpoint()
The function refreshes the buffered output and other buffered data to the Web browser. Pairsleep(.1)
So that the code is aborteddebugBreak()
Previously, the server had enough time to refresh the data to the Web browser. This function is an internal function extended by the PHP debugger downloaded earlier. In this way, callbreakpoint()
The HTML block,print()
Andecho()
The statement data is refreshed to the browser, and the code execution is aborted.
After writing the code like in listing 4, you can open the browser and point to test4.php, or you can view the PHP browser window (my http: // localhost/debugArticle/test4.php ). Each time a file is input and saved, the debugging sequence is started in the PHP browser window. If you are not using Windows, check test4.php in your browser. After saving the file, useF8Or clickRun> ResumeContinue code execution. Continue to do this until the output in the last row isEND!
(See Figure 9, 10, and 11 ).
Figure 9. initial PHP browser output to the first breakpoint
Note how to display the execution as suspended in the Debug window in Figure 9.
Figure 10. PHP browser output after the first breakpoint and before the second breakpoint
The Debug window in Figure 10 still shows the execution as suspended, while the second set of data is displayed in the PHP browser.
Figure 11. complete PHP browser output
Note: the code in the Debug window in Figure 11 is no longer suspended, and the entire script has been executed, as shown in the PHP browser in Figure 11.
Now that you have seen the advantages of developing with PHPEclipse and debugger extensions, it is hard to imagine what will happen without it.
Conclusion
Now you have added application of error reports, print statements, PHPEclipse, and debugger extensions to PHP debugging technology. you can reduce the number of errors in each line of code, become more effective PHP coding personnel. See references for some PHP tutorials to test these new skills.
Download Sample code for PHP Debugging
References
Learning
- For more information, see the original article on the developerWorks global site.
- Learn how®Install Java on the system.
- Visit Eclipse.org to obtain comprehensive information about programming and how to use it.
- "Getting started with the Eclipse platform" (developerWorks, November 2002) provides a history and overview of Eclipse, including details on how to install Eclipse and plug-ins.
- Please visit PHPEclipse to learn more about installing PHPEclipse and how to use it.
- DBG is a full-featured PHP debugger engine and an interactive tool that helps Debug PHP scripts. Please read this tutorial on Installing and locking ing the debugger.
- To learn more about Eclipse, visit the Eclipse project resources on developerWorks.
- To learn more about PHP, visit the PHP project resources on developerWorks.
- See the PHP Manual for more information.