Debug PHP source code in netbeans ide

Source: Internet
Author: User
Tags php source code netbeans
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:

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

     

     

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

  1. Start the IDE, run the project, and then open the source that contains the debuggingCode.
  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 ".
    4. 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. 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.
  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. 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 .
  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 + $ factorial

      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 ).
Related Article

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.