A tech blogger who turns logical thinking into code

Xdebug+phpStorm installation and debugging tutorial!

1. What is Xdebug?

Xdebug is an open source PHP program debugger, in fact, a Debug tool can be used to trace, Debug, analyze the current health of PHP programs!

2. Why use Xdebug?

Maybe in the middle of a project when your business code is so complex that it’s nested layer after layer, If print_r, log, var_dump are not enough for you, and you have not configured Xdebug in phpStorm, I suggest you try Xdebug.

3. How do I download and install Xdebug 2.7.2 in Windows

1. Download Xdebug 2.7.2

Note: When you download XDEBUG, there are two versions, one with TS, and one without TS. “TS” means thread-safe, depending on whether the PHP version supports thread-safe versions!

So we need to know what version of PHP you currently have installed! My version is PHP7.3.16 with TS

How do I check the current PHP version

Create a new test.php file in the IDE editor and type the following code:

echo phpinfo();
Copy the code

After running, see the following figure:

Xdebug 2.7.2 is an official download

Once you know the current PHP version and so on, you can download XDebug

Open the official address :https://xdebug.org/ and click the Install menu option

Then go to Installing on Windows below and click Download

Then go to the Download selection page and select Historical Releases at the bottom of the download page as shown:

I went to the Historical Releases page and selected Xdebug 2.7.2 with the release date of 2019-05-06

Note: Xdebug3.x is not recommended here for the following reasons:1.The configuration parameters of 3.0 are written slightly differently than those of 2.0, and have been completely revised2.If you have a younger version of phpStorm, the checking script for phpStorm may not have been fully updated using the Xdebug3.x version and there are compatibility issuesCopy the code

Automatically analyze the xdebug version of your system

Of course, if you don’t really know the current PHP version, too lazy to go to again then try this address: https://xdebug.org/wizard, this is the xdebug official website provide users with an automatic analysis of the current computer system corresponding to the xdebug version of the page! Open it and you will see the following page!

In this page, use CTRL +A to paste the PHP version information printed by typing phpInfo () in the IDE into A multi-line text box, and then click on my PHPInfo () output to submit your PHP information It will help you figure out which version of XDebug is best for you!

As shown in figure

This feature is handy for generating the xdebug version you’re looking for,

On the other hand, while this will help you figure out the appropriate xDebug version, the detected version is not necessarily the correct one! If something goes wrong, try several lower versions!

Php_xdebug-2.7.2-7.3-vc15-x86_64 php_xdebug-2.7.2-7.3-vc15-x86_64

2. Install Xdebug2.7.2

  1. Move the downloaded php_xdebug-2.7.2-7.3-vc15-x86_64. DLL file to X:\php7.3.16\ext

    Of course, if you are a one-click PHP environment such as WAMP, XAMpp, etc., you can also find the corresponding PHP version folder under ext directory.

  1. findphp.iniThe file editorX: \ php7.3.16 \ PHP ini

Open the php.ini file and add the following code:

[xdebug]
; Load the Xdebug library file
zend_extension = "X: \ php7.3.16 \ ext \ php_xdebug 2.7.2-7.3 - vc15 - x86_64. DLL." "
Copy the code

Note:

1. The X here refers to your own hard drive directory

2. Code can be added directly to the end of the php.ini file

3.php.ini files add lines of code with Spaces before and after the equals sign. Values can be quoted or not quoted, but I personally recommend it

  1. Be sure to rebootapacheWeb server

Here to download and install enabled completes the xdebug, back in the PHP code to print a phpinfo () function in the open information page if appears below effect to install correct!

4. Common configuration parameters of xdebug2. x

After xDEBUG is installed and started, some parameters need to be configured for it. My common configuration parameters are as follows:

parameter value Recommended values describe
xdebug.auto_trace On/OffOn/Off On Whether openAutomatic tracking
xdebug.show_exception_trace On/OffOn/Off On Whether openAbnormal tracking
xdebug.remote_autostart On/OffOn/Off On Whether openRemote debugging starts automatically
xdebug.remote_enable On/OffOn/Off On Whether openRemote debugging
xdebug.remote_host IP address or localhost localhost Client IP allowed for debugging
xdebug.remote_port (Default 9000) 9001 Remote debugging port, if the default port is occupied can be changed to other ports!
xdebug.remote_handler dbgp dbgp Application layer communication protocol for remote debugging of Zend Studio
xdebug.collect_vars On/OffOn/Off On Whether to collect variables
xdebug.collect_return On/OffOn/Off On Whether to collect return values
xdebug.collect_params On/OffOn/Off On Whether to collect parameters
xdebug.trace_output_dir An absolute path X: \ directory Trace the debug data output path
xdebug.profiler_enable On/OffOn/Off On Whether openDebugging content
xdebug.profiler_output_dir An absolute path X: \ directory Debugging result output path
xdebug.max_nesting_level The numerical 10000 Function recursive call itself times, set too small will exceed the maximum nesting error
. . .

My configuration in php.ini is as follows

[xdebug]
; Load the Xdebug library file
zend_extension = "E: \ php7.3.16 \ ext \ php_xdebug 2.7.2-7.3 - vc15 - x86_64. DLL." "

xdebug.auto_trace="On"
xdebug.show_exception_trace="On"
xdebug.remote_autostart="On"
; Enabling remote Debugging
xdebug.remote_enable = "1"
; The client IP
xdebug.remote_host = "localhost"
; Client XDEBUG listens for ports and debug protocols
xdebug.remote_port = "9001"
; Application layer communication protocol for remote debugging of Zend Studio
xdebug.remote_handler = "dbgp"
xdebug.collect_vars="On"
; Whether to enable debugging content
xdebug.profiler_enable = "On"
xdebug.trace_output_dir="E:\xdebug_tmp\debug.log"
xdebug.profiler_output_dir="E:\xdebug_tmp\debug.log"
Copy the code

5. How to configure Xdebug2.x in phpStorm

1. Set the port number

PHP—->Debug in the display panel to set the port. The default port is 9000. What we configured in php.ini is set here!

As shown in figure

2. Set DBGp Proxy

Open File >Settings >Languages & Frameworks –>PHP—->Debug—->DBGp Proxy

Configure the DBGp Proxy as follows:

Idekey is the name set in the php.ini configuration file (it doesn’t matter if it’s not in the configuration file!).

Host is your server’S IP address or an already resolvable domain name, or you can write localhost or 127.0.0.1 locally

Port can be selected, usually the default value is 80

As shown in figure

3. Manually add a Server

Open File– >Settings– >Languages & Frameworks –>PHP—->Servers

Fill in the following information in the dialog box that appears:

Name fill in a name optional!

Host Specifies the current server IP address or localhost

Port is the default port 80, or you can change the web port number according to your configuration

The debug choose xdebug

4. Test whether the configuration of Xdebug2. x is successful.

Open File again –>Settings– >Languages & Frameworks –>PHP—->Debug

Then click the Validate button on the right as shown below:

The Validate Debugger Configuration on Web Server dialog box pops up

Create a debug service for synchronization with the real server

Path to Create Validation Script: Fill in the Path to create the Validation script, that is, your service site Path address

Url to Validation Script: The Url of the validation script is your resolved domain name web address directory

The diagram below:

Here we in phpSrorm configuration xdebug2. x is complete!


6. Test using xdebug2.x locally to debug PHP code

Create a new page in phpStorm called index.php and type the following code

for($i=0;$i<5;$i{+ +)echo $i;
}

echo "hello world";
echo phpinfo();
Copy the code

Then go to the Run menu option —–>Debug and select index.php(PHP Script) in the pop-up dialog.

Open the Debug listener button in the upper right corner of phpStorm IDE as shown below

Now you can start breakpoint debugging! For example, break the point mark at the point where the breakpoint is to be broken

Then press Shift +F9 on your keyboard or directly click the bug icon in the upper right corner of phpStorm IDE

To debug breakpoints, press F9 or Resume Program to debug basic breakpoints

I will update you on the details of xdebug usage in the future!