Directory
- Preparatory work
- How to use XDebug for PHP debugging in NetBeans IDE
- Debugging options
- Working with toolbars and editors
- debugger window
- Debugging sessions
- Sample Debug Session
- Use other monitoring
- PHP and HTML Hybrid use cases
- Path mapping, debugger proxy, and starting a debugging session on a custom URL
To learn this tutorial, you need the following software and resources.
Software or Resources |
Required Version |
NetBeans IDE |
PHP Download Package |
PHP engine |
Version 5 |
Web Server |
It is recommended to use Apache HTTP Server 2.2. |
PHP Debugger |
XDebug version 2.0 or later |
Preparatory work
To successfully debug a PHP application in the NetBeans IDE for PHP, you need to install and configure the PHP engine, the Apache local Web server, and the XDebug Debugger for PHP development. If you have difficulty making XDebug work properly, please refer to the NetBeans XDebug Wiki and/or consult the community via [email protected].
How to use XDebug for PHP debugging in NetBeans IDE
When you run XDebug from the NetBeans IDE, the PHP program is paused on each line where the breakpoint is set. When the program pauses execution, XDebug can retrieve information about the current program state, such as the value of the program variable. In fact, this process can be represented by the following workflow:
- Set breakpoints on each line that should suspend execution of the PHP source code.
- Starts a debugging session.
- When you reach a row that contains a breakpoint, you can press F7 and F8 to execute the script line by row. Monitors the state of the application in the debugger window.
- Closes the debugging session.
For more information about workflows that use XDebug in the NetBeans IDE, see Debugging sessions.
NetBeans IDE provides a debug toolbar to help you step through your files. See Working with toolbars and editors.
Debugging options
The NetBeans IDE's options include a label that changes some of the default settings for PHP debugging. To turn on these options, go to Tools > Options (on your Mac, go to NetBeans > Preferences), then select the PHP option, and then choose the "Debugging" button. (debug) label.
Note: the "Debugging" (debug) label was introduced in NetBeans IDE version 7.1. Earlier versions of NetBeans have debug options in the General PHP tab. Some of the options in version 7.1 are not available in earlier versions.
You can change the following options in this panel:
For the purposes of this tutorial, you do not need to change any of these settings unless you selectively enable monitoring.
Working with toolbars and editors
You can use the editor to view the contents of a file. Therefore, when you are debugging, the editor and Debugger toolbars give you the ability to step through the code during execution to see how the contents of the file affect the actions performed in the browser.
- Using the Debugger toolbar
- Set breakpoints
- Check tool Tips
Using the Debugger toolbar
When you run a debugging session, the debugger toolbar appears above the editor.
The toolbar provides the following actions:
Complete Session () |
Completing the debugging session |
Pause () |
Suspending a debugging session |
Recovery () |
Continue the debugging session |
Step Over () |
Crossing execution Statements |
Step into () |
Step Into function call |
Step Out () |
Step out of the current function call |
run to Cursor position () |
Run To Cursor position |
Set breakpoints
Set breakpoints in the file to notify the debugger where to stop during execution.
Important: you must set breakpoints in your PHP code to use XDebug.
To set a breakpoint, in the editor, click the left margin of the line where you want to set the breakpoint.
You can delete a breakpoint by clicking the breakpoint marker ().
In addition, breakpoints can be temporarily disabled. To do this, right-click the breakpoint marker and uncheck "Breakpoint" (breakpoint) >? Enabled (? enable). This toggles the breakpoint to a disabled state, causing a gray mark () to appear in the left margin.
If the debugger encounters a breakpoint at execution time, it stops at the breakpoint so that you can examine the variable in the Debug window and then step through any code that follows the breakpoint.
Check tool Tips
When you suspend the debugger during a debugging session, you can hover the mouse over the PHP identifier in the editor to display the ToolTip. If the identifier is valid in the selected call stack frame, its value is displayed. In addition, you can select the PHP expression. The value of the expression is displayed in a ToolTip.
debugger window
When you start a debugging session, a set of debugger windows opens under the Main editor window. In the debugger window, you can track variables and expression values as you step through the code, check the call stack of the execution thread, validate the source URL, and switch between sessions if you are running a concurrent debugging session.
- "Sessions" (session) window
- "Variables" (variable) window
- "Watches" (watch) window
- Call Stack Window
- "Threads" (thread) window
- "Sources" (source) window
- "Breakpoints" (breakpoint) window
All debugger windows can be accessed from the IDE's Window > Debugging (Debug) menu. After the debugging session is active, you can start using the debugger window.
"Sessions" (session) window
The Sessions window displays all the debugging sessions that are currently active. When you start a PHP debugging session, you can see the PHP debugger entry in the Sessions window.
NetBeans IDE also allows you to run multiple debugger sessions at the same time. For example, you can debug both Java and PHP projects. In this case, you can identify the two sessions that are listed in the "Sessions" (session) window.
The current session (that is, a session that you can control with the debugger toolbar) is indicated by a more visible icon (). To switch sessions, you can either double-click the session you want to activate, or right-clicking a non-current session and selecting Activate.
Note: If you suspend the current session, we recommend that you do not switch sessions.
You can also use the right-click Pop-up window to terminate the session (right-click and select Finish), or switch between the current thread in the debug session or debug all threads (right-click and select Scope > Debug All Threads "(Debug All Threads) or" Debug Current Thread "(to debug the active threads).
"Variables" (variable) window
After the debugger is suspended, the Variables window displays the variables for the current object of the selected call stack frame window
. In the current window, the node for each variable is displayed. The hyper-global variables are grouped by separate nodes.
The values of some local variables may change as you step through the code. Such local variables are shown in bold in the local Variables window. You can also click the Values column directly and change the value of the variable manually.
"Watches" (watch) window
Setting up monitoring can cause XDebug instability, so this is not recommended. By default, monitoring is disabled. However, if you want to set up monitoring, see using other monitoring.
Call Stack Window
The Call Stack window lists the sequence of calls made during execution. When the debugger is suspended, the Call Stack window displays a sequence of function calls (that is, the call stack ). At the first pause, the topmost stack frame is automatically selected. Double-click the function call in the window to go to the line in the editor. If you make a call to a PHP class, when you double-click the call, the Navigator window will also go to that row.
You can double-click a call stack frame to select it, and then view any variables or expression values for that frame in the Variables (variable) and Watches (watch) Windows.
"Threads" (thread) window
The "Threads" window indicates which PHP script is currently active and whether it hangs at the breakpoint or is in a running state. If the script is running, you need to go to the browser window and interact with the script.
"Sources" (source) window
The Sources window displays all the files and scripts loaded for the debugging session. For PHP projects, the "Sources" window does not currently work.
"Breakpoints" (breakpoint) window
You can use the Breakpoints window to view all breakpoints that are set in the IDE.
Breakpoints can be enabled or disabled in the context window through the Breakpoints window. In addition, you can create a breakpoint group.
Debugging sessions
The following procedure is a workflow for a typical debugging session.
To run a debugging session:
- Start the IDE, and then open the file that contains the source code that you want to debug.
- Set a breakpoint on each line where you want to pause the debugger. To set a breakpoint, place the cursor at the beginning of the line and press Ctrl-f8/?-f8, or select Debug > Toggle lines Breakpoint (turn on/off row breakpoints).
- In the Projects window, navigate to the current project node, right-click, and choose Debug from the pop-up menu. The IDE opens the debugger window and runs the project in the debugger until the breakpoint is reached.
Note: If the current project is set to main project, you can choose Debug > Debug Main project, press CTRL-F5, or click.
- Switch to the local Variables window. This window displays all the variables that have been initialized in the current function and their types and values.
- To view the value of a variable outside of the function, place the cursor at a location where the variable appears. The tooltip displays the value of the variable.
- To execute a program line-by-row (including all the rows in the called function), press F7 or select Debug > Step Into, and monitor changes in the values of these variables in the local Variables window.
- To examine the program logic by monitoring the changes to the expression, define a new watch:
- To open the Watches window, choose Window > Debugging (Debug) > Watches (Watch), or press the CTRL-SHIFT-2 key combination button. The "Watches" (Watch) window opens.
- Right-click anywhere in the Watches window and choose New Watch from the pop-up menu (new Watch). The new Watch window opens.
- Enter a watch expression, and then click OK.
You can now perform additional checks during debugging.
IMPORTANT: You must enable monitoring in the Debugging tab of the PHP options (option) to set up monitoring.
- To cancel a line-by-row operation of the code in a function and jump to the next line after the function call, press CTRL-F7/?-F7 or select Debug > Step out.
- To skip a line-by-row execution of code in a function, get the value returned by the function, and jump to the next line after the function call, press F8 or select Debug > Step over.
- To pause the debugging session, select Debug > Pause. "
- To continue the debugging session, select Debug > Continue (Continue) or press.
- To cancel the debugging session, press.
At the end of the program, the debugger window closes.
Sample Debug Session
The sample in this section illustrates the basic debugger functions, including stepping and stepping functions. In addition, a typical debugger window output is displayed.
- Create a new PHP project with the following parameters:
- Project Type-PHP application
- SOURCE location-The default location for the htdocs folder
- Run Configuration-"Local Web site"
For more details on setting up a PHP project, see Setting up a PHP project document.
- To use a hotkey during a session, place the cursor over the project node and choose Set as Main project from the pop-up menu.
- Inindex.phpFile, enter the following code:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>netbeans PHP Debugging Sample</title>
<body>
<?php
$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;
function Calculate_sum_of_factorials ($argument 1, $argument 2) {
$factorial 1 = calculate_factorial ($argument 1);
$factorial 2 = calculate_factorial ($argument 2);
$result = Calculate_sum ($factorial 1, $factorial 2);
return $result;
}
function Calculate_factorial ($argument) {
$factorial _result = 1;
for ($i =1; $i <= $argument; $i + +) {
$factorial _result = $factorial _result* $i;
}
return $factorial _result;
}
function Calculate_sum ($argument 1, $argument 2) {
Return $argument 1 + $argument 2;
}
?>
</body>
The code consists of three functions:
- calculate_factorial () function
- calcualte_sum () function
- The calculate_sum_of_factorials () function, which calls the calculate_factorial () function two times, and then calls calcualte_sum () function once, and then returns the calculated factorial and.
- Set a breakpoint (Ctrl-f8/?-f8 key combination) at the beginning of the PHP block:
<?php
- To start debugging, click. The debugger stops at the breakpoint.
- Press F7 key three times. The debugger stops on the line that calls the function calculate_sum_of_factorials () . The Local Variables window displays the variables $m and $n , along with their values:
- To step into the function calculate_sum_of_factorials (), press the F7 key. The debugger starts executing the code in function calculate_sum_of_factorials () and then stops at the call of function calculate_factorial () .
The local Variables window will now display the locals declared in function calculate_sum_of_factorials () $argument 1 and $ Argument2.
- Press the F7 key. The debugger starts executing the code in the function calculate_factorial () . The Call Stack window displays the call stack of the function in reverse order, and the last function called is at the top of the list:
- Press F7 to enter the loop. View the value of the variable in the "Variables" (variable) window.
- If you are sure that the code is working correctly, press CTRL-F7/?-F7 to cancel the function execution. The program will return to the next line after calling the function calculate_factorial () .
Note: You can also press the F7 key until the program finishes executing function calculate_factorial () . You will also return to the next line after calling the function.
- Since you have just checked the function calculate_factorial ()and determined that it is functioning correctly, you can skip the function's re-execution ("Step Over"). To bypass the function, press F8. The program will stop at the call of function calculate_sum () .
- To step into the function calculate_sum (), press the F7 key.
- To bypass the function, press F8. In either case, the debugger stops at the last line of the function calculate_sum_of_factorials () .
- Press the F7 key. The debugger moves to the line on which the Echo statement is located.
- Press the F7 key until the debugger exits the program. The browser window opens and displays the results of the program execution:
Use other monitoring
You can define additional monitoring expressions to track the execution of a program. This helps catch errors.
Warning: setting up additional monitoring can cause XDebug instability. By default, monitoring is disabled, see debugging options.
- Update the code as shown below (replace the plus sign with a minus sign):
function Calculate_sum ($argument 1, $argument 2) {
return $argument 1-argument2;
}
Suppose the operator changes because of a misspelling, and you actually need to calculate and.
- Select Debug > New Watch (New Watch), or press CTRL/?-SHIFT-F7 to combine the keys. The new Watch window opens.
- Enter the following expression and click OK.
$factorial 1+ $factorial 2
The new expression will appear in the Watches window.
- Run the debugging session. When the debugger stops at the following line
return $result;
Compares the value of an expression in the Watches window with the value $result in the local Variables window. They should be the same, but they are different in this example.
This example is very simple, and it gives you some basic concepts for using monitoring. PHP and HTML Hybrid use cases
You can debug code that contains both PHP blocks and HTML blocks. In the example debugging Session section of the sample, the values are fixed-coded. The code is now extended with an HTML input form for entering values.
- Add the following HTML code under the <?php?> block, type or paste the following code:
<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/>
<inp UT type= "Submit" Name= "enter" value= "enter"/>
</form>
- Replace the <?php?> block by typing or pasting the following code:
$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". $result;
}
- Set a breakpoint at the beginning of the <?php?> block, and then start the debugging session.
- Press the F7 key. The debugger will step into the program. At the same time, the browser window opens, but the input form is not displayed. This is the correct behavior of the debugger, because it must first pass through the entire source code of the Web page before the page can be displayed. In fact, this means that the debugger has passed the code two times. For the first time, the debugger processes the code to display the HTML input form. The second time is the debugger stepping through the PHP code.
- Press the F7 key until the debugger reaches the end of the program and opens the input form.
- Fill out the form, and then click the Enter key. The debugging session will continue, as described in the Sample Debugging session section.
Path mapping, debugger proxy, and starting a debugging session on a custom URL
You can debug scripts and Web pages, and you can also debug Web pages locally or remotely. Unfortunately, for remote debugging, the PHP files that are debugged on the remote server are not the same files that are opened in the NetBeans IDE that runs on the local computer. As a result, debugger support in NetBeans must be able to map the server path to a local path. However, because there are many complex factors, the path mapping problem cannot be solved automatically for various situations. So, starting with NetBeans 6.7, you can manually define path mappings for each run configuration through the project settings. In addition, you can specify the proxy server (if any), and the URL on which to start the debugging session. If this URL is not specified, debugging is performed starting at the index file.
Set the path map and enable the custom debug URL:
- Right-click the project node in the Projects window and open the project's "Properties" from the context menu.
- In the Project Properties dialog box, go to the "Run Configuration" category.
- Click the Advanced button. The Advanced Web Configuration dialog box opens.
- Add the server path and project path to which you want to map the path.
- Under Debug URL, select one of the following options (do not leave the default values selected):
- Ask every time (ask): Let the IDE prompt you to enter a URL when you start a debugging session.
- Do not open Web Browser: Requires you to manually open the browser and enter the URL (you need to get/post the Xdebug_session_start variable).
- When debugging with a proxy server, enter the host name and port of the server in the Debugger proxy category.
For more information, see Path Mapping in PHP Debugger (path mapping using the PHP debugger) in the NetBeans for PHP blog post.
Send feedback about this tutorial
To send comments and suggestions, get support, and stay up to date with the latest developments in NetBeans IDE PHP development features, please join the [email protected] mailing list.
Debugging PHP source code in the NetBeans IDE