Debug PHP code using XDebugger in VSCode

Debug PHP code using XDebugger in VSCode
Debug PHP code using XDebugger in VSCode

It’s a hectic day and we have deadlines. Consider a situation where you have been assigned a critical error and you don’t understand the code flow at all. You are adding all your energy to trace the issue and find out the exact location and cause of the Error.

You have added too many var_dump(), print_r(); / var_export(); and die; / exit(); function to jump from one function to another and get the value of the variable.

This way of debugging is extremely frustrating and time-consuming. We want something smart that will help us to debug the code line by line and help us to solve our problem.

And the solution is XDEBUG. To be honest, when I heard about XDebug for the first time, I thought it is some kind of third-party library (did not know that it is an extension) and after importing it, again I have to use some XDebug function to show the values of the variables in a fancy way. My BAD.

After a few days, I went to youtube and searched about XDebug & then I came to know about the whole story. Gaah!

What is Xdebug?

Xdebug is a simple extension in PHP that provides debugging and profiling to the application allowing us to inspect variables/properties and check function calls one-by-one.

Prior to its use, we need to find the appropriate xdebug extension for our PHP version and add it to the ext folder under xampp folder (or whichever you are using) and update the same in the php.ini file (with some additional configurations). Once this is done, finally we need to restart the server that will compile our extension for ready to use.

Let’s see how to install XDebug at our machine and the setup.

How to install XDebug?

To install the XDebug in a Windows machine, we need to first check what version of PHP is installed in the machine and we can do that in different ways.

Using Terminal

  1. Press Ctrl + R then type cmd and hit Enter.
open cmd/terminal

2. Next, type php -v / --version to get the php version

how to know php versio using cmd/terminal

In my case, it’s PHP 7.4.9, 64 bit. But in your case, it may vary depending upon the current PHP version on which you are working. Once, we complete this step then we can see XDebug information here in the terminal along with its version and the author.

Using phpinfo()

Another way to identify the version of PHP is using PHPs inbuilt function phpinfo().

To use this method, we need to go to the htdocs folder OR whichever folder you have made as a root document folder for your project and then create a sample.php file. Write the following code inside it and then run that file in the browser.

<?php

phpinfo();
how to know php version using phpinfo

This time we get more detailed information about the PHP installed in the system along with all the configurations, extensions, and settings in it.

We can also use phpversion() function and get the version of the currently installed PHP version in the machine.

Now, we know the PHP version and the next target is the get the compatible xdebug DLL file that is required for debugging. This DLL file will be compiled once we restart the server and its functionality will be available to use.

To get the DLL file, we need to copy the output of phpinfo() (copy all the HTML version) or we can also copy all the content of php -i output (from Terminal) and paste in the provided textarea at the wizard tool and click on the Analyse my phpinfo() output button.

This will analyze our phpinfo() and accordingly provide us the exact php_xdebug.php file required for our configuration and location where we can store it.

wizard tool to inspect phpinfo

So, as per the instructions, download the provided DLL file to C:\xampp\php\ext location and then open php.ini file and add the following lines along with zend_extension (provided by wizard tool) at the end of the file like this-

[XDebug]
zend_extension = "c:\xampp\php\ext\php_xdebug-3.0.1-7.4-vc15-x86_64.dll"
xdebug.remote_host = "127.0.0.1"
xdebug.remote_port = 9000
xdebug.remote_enable = 1

Now, save php.ini and restart the server. Now, XDebug is ready to use.

To verify whether XDebug is installed properly or not, go to terminal and again type php -v / php --version and it will add an additional line that will look similar to like this.

Debug PHP code using XDebugger in VSCode

OR

Open sample.php file again in the browser and search xdebug in it. This time, you will see xdebug information in it.

phpinfo xdebug

How to configure XDebug in vscode?

Now, we are done with all the necessary steps to enable and configure XDebug in our local system. It’s time to use it inside our IDE (in our case it will be VSCode).

To enable it within VSCode, we need to install Extension PHP Debug in our IDE.

Debug PHP code using XDebugger in VSCode
php debug extension for vscode

Once installed, now we can see Debugger Icon at the left side navigation bar as shown below, and at the top, we can see Run As an option –

Debug PHP code using XDebugger in VSCode
Setup PHP XDebug

Click on create a launch.json file and it will create a configuration file for our debugger. That’s it. Now create any sample.php file with any custom code to test if a debugger is working or not.

Now, once sample.php file is created then add breakpoints by clicking on the line (shown in the image) & then select Launch currently open script from Top Left dropdown menu.

Debug PHP code using XDebugger in VSCode
adding breakpoint in vscode

Click on the play button at the Top Left corner and the debugger will stop at the breakpoint as shown in the image.

Debug PHP code using XDebugger in VSCode
VSCode XDebugger with Different sections

There are different options available that performs different operations on current code. Check all the options below-

Debug PHP code using XDebugger in VSCode

Now, let’s understand each option in detail-

Continue: It will jump to the next breakpoint (if available)
Step Over: It will jump to the next line of the current code file.
Step Into: It will jump into the function definition of the current code line. In the case of multiple functions on the same line, it will first jump into the innermost and then step by step in the outermost function of that line.
Restart: It will stop the further execution of the breakpoints and will jump to the first one.
Stop: It will completely stop the execution of the current script.

Conclusion:

Xdebuger OR PHP XDebug is the savior for the developers. It helps to find the code easily and route through different parts of the codebase easily.

It also helps developers to find the potential bug easily which eventually increases the productivity time.

We have seen set of PHP XDebug in VSCode but we can set up it in PHPStorm as well which is another popular IDE for PHP development.