Debugging PHP in PHP-NetBeans

Source: Internet
Author: User
Tags php debugger netbeans

Directory

  • Preparations
  • Enable XDebug
  • How to Use XDebug for PHP debugging
  • Debugging Area Window
  • Debugging session
  • Sample debugging session
    • Use other monitoring
  • PHP and HTML hybrid Use Cases

To learn this tutorial, you must have the following software and resources.

Software or resources
Required version

NetBeans IDE
PHP
PHP engine version 5

Web Server
Apache HTTP Server 2.2 is recommended.

PHP Debugger
XDebug 2.0 or later

Preparations

To successfully debug the PHP application in NetBeans IDE for PHP, You need to develop, install, and configure the PHP engine, Apache Local Web server, and XDebug Debugger for PHP development.

Enable XDebug

After XDebug is installed, you must enable it in the PHP engine.

Enable XDebug:

  1. FindPhp. iniFile. If you runPhpinfo ()This File is displayed in the "Loaded Configuration File" (the configured File) header of the first table.
  2. 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.

  3. If you use the XAMPP package, disable the Zend optimizer because it blocks XDebug. Locate related rows, delete them, or mark them as comments.

    Note:If XAMPP Lite is used, you needPhp. iniFile:

    • /Xampplite/php. ini
    • /Xampplite/apache/bin/php. in
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:

  • You can set a breakpoint on the line where PHP source code execution should 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:

  1. Start the IDE, run the project, and open the file containing the source code to be debugged.
  2. Place the cursor on a line and you want to automatically execute the PHP script before that line.
  3. To set breakpoints, press the Ctrl-F8 key combination or choose debug> enable/disable line breakpoints ".
  4. 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.
  5. Switch to the local variable window. This window displays all the variables initialized in the current function and their types and values.
  6. To view the variable value outside the function, place the cursor at a position where the variable appears. The tooltip displays the variable value.
  7. 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.
  8. To check the program logic by monitoring expression changes, define a new monitor:
    1. To open the monitoring window, choose Window> debug> monitor, or press the Ctrl-Shift-2 key combination. The monitoring window opens.
    2. Right-click any position in the monitoring window and select new monitoring from the pop-up menu ". The "New monitoring" window opens.
    3. Enter the monitoring expression and click OK ". Now you can perform other checks during debugging.
  9. 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 ".
  10. 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 ".
  11. To pause a debugging session, select "debug"> "pause ".
  12. To continue the debugging session, select "debug"> "continue" or press.
  13. To cancel the debugging session, follow.
  14. When the program ends, the debugger window is closed.

Sample debugging session

This section describes a standard debugging session.

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

  2. 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.
  3. 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 ".
  4. InIndex. phpFile, enter the following code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  

    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.
  5. Set a breakpoint at the beginning of the PHP block (Ctrl-F8 ):
    <?php
  6. To start debugging, click. The debugger stops at the breakpoint.
  7. Press F7 three times. The debugger will call the FunctionCalculate_sum_of_factorials (). The "local variables" window displays variables.$ MAnd$ NAnd their values:

  8. Function entryCalculate_sum_of_factorials (), Press F7. The debugger starts executing the function.Calculate_sum_of_factorials ()And then in the functionCalculate_factorial ().


    Now, the "local variables" window displays the FunctionCalculate_sum_of_factorials ()Local variables declared in$ Argument1And$ Argument2.

  9. 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:

  10. Press F7 to enter the cycle. You can view the variable value in the "local variables" window.

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

  12. 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 ().

  13. Function entryCalculate_sum (), Press F7.
  14. To go through this function, press F8. In either case, the debugger willCalculate_sum_of_factorials ().

  15. Press F7. The debugger movesEchoStatement.
  16. 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.

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

  2. Choose debug> new monitoring or press the Ctrl-Shift-F6 key combination. The "New monitoring" window opens.
  3. Enter the following expression and click OK ".
    $factorial1+$factorial2

    The new expression is displayed in the monitoring window.

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

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

  2. 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;
    }
  3. In <? Php?> Set a breakpoint at the beginning of the block, and then start the debugging session.
  4. 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.
  5. Press F7 until the debugger reaches the end of the program and opens the input form.
  6. Enter the form and click Enter. The debugging session will continue (as described in the sample debugging session section ).

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: info-contact@alibabacloud.com 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.