PHP debugging Guide

Source: Internet
Author: User
Tags configuration settings php debugger php error




I. Prerequisites

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, 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.

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)

). Debugging PHP applicationsProgramYou should know two configuration variables. The two variables and their default values are 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.Display_errors

The purpose of the variable is obvious-It tells PHP whether an error is displayed. The default value isOff

. However, to make the development process easier, set this valueOn


Display_errors = on



The default value of the variable isE_all

. This setting displays all information from poor coding practices to harmless prompts to errors.E_all

The development process is a little too detailed, 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 following valuesError_reporting

Default Value:

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 distinguish
Which PHP version Apache is using, because Apache can only view one PHP. ini file. I don't know which Apache is using
Configuring the php. ini file is 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 should have been exposed to the basic configuration in the HTTP. conf file in <apache2-install-dir>/CONF/httpd. conf.

To add the following lines to the httpd. conf file to overwrite any PHP. ini file:

Php_flag display_errors on
Php_value error_reporting 2039


This will be overwritten in the PHP. ini fileDisplay_errors

The configured flag andError_reporting

. Value2039

RepresentativeE_all &~ E_notice

. If you want to useE_all

, Set the value2047

. 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 pointCode. Create a simple PHP file test. php and define it as shown in Listing 1.

Listing 1. A simple PHP with incorrect generation

<? PHP
Print ("the next line generates an error. <br> ");
Printaline ("please? ");
Print ("This will not be displayed due to the above error .");

FirstPrint ()

The 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 causes the lastPrint ()

The statement does not work, 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 the functional bugs in the application do not produce errors, in all debugging policies, how to correctly place and usePrint


Statements to debug PHP applications is a good asset. AvailablePrint

The statement can 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 the processing is passedGet

The form data sent by the request to display information to the browser. For some reason, the data is not submitted correctly or cannot be correctlyGet

Read in the request. To debug such problems, it is important to usePrint ()

OrDie ()

The statement knows the value of the variable.

Die ()

The statement terminates the program execution and displays the text on the web browser. If you do not want to comment out the code, and only want to display the information before and after the errorDie ()

Statements are 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 I was developing an application on Linux, there was no convenient GUI to tell me where the bug was. I quickly found out that
The more print statements, the more chance I will narrow down the bug range to a row in the application. Create another PHP file test2.php, as shown in Listing 2.
Define it as shown in.

Listing 2. Show all variables submitted through 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">


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 extractsGet

All the variables in the request, if any, are displayed in the browser. A form is also provided, usingGet

Request to send a variable to the server for testing. See output, 2.

Figure 2. Output of test2.php

ClickSend GET request

Button, 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

Foreach ($ _ get as $ key => $ I ){
Print ("correct data? ". $ _ Get [$ key]." <br> ");
Print ("$ key = $ j <br> ");


The print statement is in bold. Note that$ Key

The value 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
Bug. 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 using databases is to output SQL
Statement to make sure that the executed SQL is 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.


Back to Top


Use phpeclipse

You may have used eclipse, but may not be familiar with it. See references

Get 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 ProjectDebugarticle

Right-click, select properties, and clickPHP Project Settings

. ClickConfigure workspace settings

Then modify the appropriate localhost or add the Web server listening port (for example, http: // localhost: 8080 ). ClickApply

Complete 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.

<? PHP
Print (, "Hello world! ");


Note that the two characters underlined in Listing 4 are underlined in eclipse, prompting that the syntax is incorrect. PressCTRL + S

Save 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 + S

Save 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.


Back to Top


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 goFile

Tab, enter the current projectDebugarticle

And files to be debuggedTest4.php

. Now goEnvironment

Tab, and thenInterpreter

Sub-tab. Find the php.exe file in the PHP installation directory (my files are C:/apps/php5.0.3/php.exe ). ClickRemote debug

Sub-tab, selectRemote debug

If windows is not used, cancel "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.
C:/www/debugarticle/test4.php ). ClickDebug


Now the debug perspective should be loaded, as shown in figure 8. Otherwise, clickWindow> open perspective> Other

And 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

<? PHP
Function break-point (){
Ob_flush ();
Flush ();
Sleep (. 1 );
Debugbreak ();
Print ("This will get shown first ,");
Print ("as will this <br> ");
Breakpoint ();
Print ("this won't get shown until after ");
Print ("continuing the break-point <br> ");
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 as in Listing 4, you can open the browser and point to test4.php, or you can view the PHP browser window (My is
Http: // localhost/debugarticle/test4.php ). Every time you input and save a file
The debugging sequence has been started in the browser window. If you are not using Windows, check test4.php in your browser. After saving the file, useF8

Or clickRun> resume

Continue 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.