Preparations
To successfully debug the PHP application in the netbeans IDE for PHPProgramFor PHP development and installation, you must configure the PHP engine, Apache Local Web server, and xdebug debugger.
Enable xdebug
After xdebug is installed, you must enable it in the PHP engine.
Enable xdebug:
-
- FindPHP. iniFile. If you runPhpinfo ()This file is displayed in the "loaded configuration file" (the configured file) header of the first table.
- InPHP. iniTo uncomment or add the following lines:
Zend_extension_ts = "<path to the PHP folder>/php_xdebug-2.0.2-5.2.5.dll" xdebug. remote_enable = 1xdebug. remote_host = 127.0.0.1xdebug.remote _ Port = 9000xdebug. remote_handler = dbgp
For more information about how to configure xdebug, click here.
Note:Make sure that the path you specified matches the location of the corresponding file determined during installation.
- If you use the XAMPP package, disable the Zend optimizer because it blocks xdebug. Locate related rows, delete them, or mark them as comments.
- /Xampplite/PHP. ini
- /Xampplite/Apache/bin/PHP. In
Note:If XAMPP Lite is used, you needPHP. iniFile:
How to Use xdebug for PHP debugging
IfPHP. iniXdebug is enabled in the file, so that xdebug can pause and resume PHP program execution at any time. When the program is paused, xdebug can retrieve information about the current program status, for example, reading the value of the program variable. In fact, this process can be represented by the following workflow:
- Users in PHPSource codeSet a breakpoint for the row to be paused.
- When this row is reached, you press F7 and F8 to execute the script row by row and check the value of the variable.
Debugger window
When a debugging session is started, a set of debugger windows are opened in the main editor window. To add a new window, select "window"> "debug ". You can use the following window:
- The "local variables" window displays the list of initialized variables and their types and values.
- The "monitoring" window displays a list of user-defined expressions and their values.
- The "call stack" window displays the list of called functions in reverse order. The last called function is displayed at the top of the list.
- The "breakpoint" window displays the file where the row with the breakpoint is set and the list of row numbers.
- The "session" window displays the list of debugging sessions for the current activity.
- "Thread" Window
- "Source" Window
- "HTTP client monitor" Window
Debugging session
The following process is a typical workflow for debugging sessions.
Run the debugging session:
- Start the IDE, run the project, and then open the source that contains the debuggingCode.
-
- Place the cursor on a line and you want to automatically execute the PHP script before that line.
-
- To set breakpoints, press the Ctrl-F8 key combination or choose debug> enable/disable line breakpoints ".
-
- In the "project" window, navigate to the current project node, right-click, and select "debug" from the pop-up menu ". IDE opens the debugger window and runs the project in the debugger until the breakpoint is reached.
Note:If the current project is set as the master project, you can select debug> debug master project, press the Ctrl-F5 key combination or click.
-
- Switch to the local variable window. This window displays all the variables initialized in the current function and their types and values.
-
- To view the variable value outside the function, place the cursor at a position where the variable appears. The tooltip displays the variable value.
-
- To execute the program line by line (including all the rows in the called function), press F7 or select "debug"> "Step ", then, monitor the changes to these variable values in the local variables window.
- To check the program logic by monitoring expression changes, define a new monitor:
- To open the monitoring window, choose Window> debug> monitor, or press the Ctrl-Shift-2 key combination. The monitoring window opens.
- Right-click any position in the monitoring window and select new monitoring from the pop-up menu ". The "New monitoring" window opens.
- Enter the monitoring expression and click OK ".
Now you can perform other checks during debugging.
-
- To cancel a row-by-row operation on the code in a function and jump to the next line after the function call, press the Ctrl-F7 key combination or select debug> step-out ".
-
- To skip the row-by-row operation on the code in a function, obtain the value returned by the function, and jump to the next row after the function is called, press F8 or select "debug"> "Step ".
-
- To pause a debugging session, select "debug"> "pause ".
-
- To continue the debugging session, select "debug"> "continue" or press.
-
- To cancel the debugging session, follow.
When the program ends, the debugger window is closed.
Sample debugging session
This section describes a standard debugging session.
-
- Use the following parameters to create a new PHP project:
- Project type-PHP application
- Source location-HtdocsDefault Folder location
- Run configuration-Local Web Site
For more information about setting up a PHP project, see setting up a PHP project document.
-
- After the project is created, place the cursor on the "source file" node, select "properties" from the pop-up menu, and open the "debug" window. Select the "Debug server PHP" check box.
- To use a hot key during a session, place the cursor on the project node and select "set as the master project" from the pop-up menu ".
- In Index. php File, enter the following code:
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> The Code contains three functions:
- Calculate_factorial ()Function
- Calcualte_sum ()Function
- Calculate_sum_of_factorials ()Function. This function is called.Calculate_factorial ()The function is called twice.Calcualte_sum ()Function once, and then return the factorial and of the calculation.
- Set a breakpoint at the beginning of the PHP block (Ctrl-F8 ):
<? PHP
-
- To start debugging, click. The debugger stops at the breakpoint.
-
- Press F7 three times. The debugger will call the FunctionCalculate_sum_of_factorials (). The "local variables" window displays variables.$ MAnd$ NAnd their values:
- enter the function calculate_sum_of_factorials () , press F7. The debugger starts to execute the function calculate_sum_of_factorials () , and then in the function calculate_factorial () is stopped.
now, the "local variables" window displays the functions calculate_sum_of_factorials () local variables declared $ argument1 and $ argument2 .
- Press F7. The debugger starts executing the function.Calculate_factorial (). The "call stack" window displays the function call stack in reverse order. The called function is located at the top of the list:
-
- Press F7 to enter the cycle. You can view the variable value in the "local variables" window.
-
- If you are sure the code runs properly, press the Ctrl-F7 key combination to cancel function execution. The program will call the FunctionCalculate_factorial ()And then return to the next row.
Note:You can also press F7 until the program finishes executing the function.Calculate_factorial ()So far. You will also return to the next row after calling this function.
- Because you have just checked the FunctionCalculate_factorial ()And make sure that the function is running normally. Therefore, you can skip the next operation on the function ("Step by Step "). To go through this function, press F8. The program willCalculate_sum ().
-
- Function entryCalculate_sum (), Press F7.
-
- To go through this function, press F8. In either case, the debugger willCalculate_sum_of_factorials ().
-
- Press F7. The debugger movesEchoStatement.
- Press F7 until the debugger exits the program. The browser window is opened and the program execution result is displayed:
Use other monitoring
You can define other monitoring expressions to track program execution. This helps capture errors.
- Update the Code as follows (replace the plus sign with the minus sign ):
Function calculate_sum ($ argument1, $ argument2) {return $ argument1-argument2 ;}
Assume that the operator is changed due to spelling errors, but you actually need to calculate and.
- Choose debug> new monitoring or press the Ctrl-Shift-F6 key combination. The "New monitoring" window opens.
- Enter the following expression and click OK ".
$ Factorial1 + $ factorial
The new expression is displayed in the monitoring window.
- Run the debugging session. When the debugger stops in the following line
Return $ result;
Compare the expression value in the monitoring window with the value of $ result in the local variable window. They should be the same, but they are different in this example.
This example is very simple and provides you with some basic concepts of using monitoring.
PHP and HTML hybrid Use Cases
You can debug code that contains both PHP blocks and HTML blocks. In the example of the sample debugging session, the value is fixed encoding. The code is now extended through the HTML input form for the input value.
-
- In <? PHP?> Add the following HTML code above the block:
<Form action = "index. php" method = "Post"> enter the first integer, please:
<Input type = "text" name = "first_integer"/> <br/>
Enter the second integer, please:
<Input type = "text" name = "second_integer"/> <br/>
<Input type = "Submit" name = "enter" value = "enter"/>
</Form>
For more information, see the HTML input form.
- Set <? PHP?> The following lines at the top of the block:
$ M = 5; $ n = 10; $ sum_of_factorials = calculate_sum_of_factorials ($ M, $ n); echo "The sum of factorials of the entered integers is". $ sum_of_factorials;
Replace with the following code:
If (array_key_exists ("first_integer", $ _ post )&&
Array_key_exists ("second_integer", $ _ post) {$ result = calculate_sum_of_factorials ($ _ post ["first_integer"], $ _ post ["second_integer"]);
Echo "sum of factorials is". $ sum_of_factorials;
}
-
- In <? PHP?> Set a breakpoint at the beginning of the block, and then start the debugging session.
-
- Press F7. The debugger will step into the program. At the same time, the browser window is opened, but the input form is not displayed. This is the correct behavior of the debugger, because it must first pass the entire source code of the web page before displaying the page. In fact, this means that the debugger has passed the code twice. For the first time, the debugger processes the code to display the HTML input form. The second time, the debugger gradually executes PHP code.
- Press F7 until the debugger reaches the end of the program and opens the input form.
-
- Enter the form and click Enter. The debugging session will continue (as described in the sample debugging session section ).