How to Debug PHP Using Xdebug On Vscode
Tags: php, vscode, xdebug, devops
Published: 2021-09-10 12:13:15



### Introduction

It's important to keep a few things in mind while developing web apps. 
When it comes to building large and powerful applications, a competent developer will always follow best practices. Testing, debugging, performance, and security of websites or apps are some of these techniques. 
Using VSCode and the PHP xDebug module, you'll enable full-featured functional debugging throughout your whole application.

## Prerequisites

The following criteria must be met in order to follow along with this guide:

* A PHP development environment that runs at least PHP 7.0.
* An appropriate version of [Visual Studio Code](https://code.visualstudio.com/#alt-downloads) for your operating system 
 
## Step 1 — How to Download and Install the XDebug Tool

Real-time debugging is made possible using Xdebug, a PHP extension. On the other hand, it can be useful for troubleshooting server-side problems. Since it shouldn't be active on most requests, Xdebug isn't enabled in the same manner as the other extensions are.You would install it following the steps below:

#### Windows

Xdebug provides a wizard tool for Windows users to obtain a `.dll` file. In order to do this, first create a PHP file in a folder, and then add the following code line:
```php
<?php 
phpinfo();
?>
```
Use your browser to launch the `index.php` file . This will provide the information displayed in the image below.
![php info](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5skbxjv9egtv64hqrxq.png)
* Select and copy all information on the page.
* You'll next need to go to the XDebug [Wizard tool](https://xdebug.org/wizard.php) and paste the information into a Text Box input field.

* Look for 'Analyze my phpinfo() output' and click it.
![Xdebug wizard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz7s3xcvzz3prm7h3n4g.png)
* There is a module that may be downloaded under Instructions, number 1. As you can see in the image below:
![Instruction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0l9e78lmomvui8ae8mb.png)
Please note that the path to the directory in this example may be different from yours, so please pay attention to the directions in the image above.
#### MAC
You can install PHP7 Xdebug using pecl extension:
```command
pecl install xdebug
```
#### Linux
On Linux distribution like fedora 32, you can use Sudo to install Xdebug in PHP:
```command
sudo yum install php-xdebug
```
you can refer to the [xdebug](https://xdebug.org/docs/install) docs if you use other linux distribution.
It's time to enable Xdebug by adding the following code to file `xdebug.ini` under ``etc/php/7.0/mods-available/``
```ini
xdebug.profiler_enable_trigger = 1
xdebug.profiler_enable = 0
xdebug.remote_enable = 1
xdebug.profiler_output_dir = "/tmp"
```
Now restart your server.


## Step 2 — Enabling the XDebug extension in PHP

Microsoft's Visual Studio Code (VS Code) is a free open source text editor. VS Code is available for Windows, Linux, and Mac. There are significant features in the editor that make VS Code one of the most popular programming environments in recent years, despite its size.
Firt

* Launch VS Code

* Install the Felix Becker PHP Debug extension. The PHP Debug extension may be found in VSCode's Extensions tab by searching for it.
![PHP DEBUG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbnqixt6qa7wtmvylj4k.png)
* Click on the `Run` tab and click on add configuration.
![Run Tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6tl0ctfvzlg1kudpycn.png)
Now, you'll need to pick the PHP environment. A new `launch.json` file will be added to the root directory by VSCode.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wr61mgfmvd2za9et74o2.png)
Finally, add the `runtimeExecutable` property to the list after port replacing `path_to_php_executable` with the path to your php executable:
```json
"runtimeExecutable": "path_to_php_executable"
```
![Launch.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbcoekvtknptin3sh0j8.png)
Please save the file `launch.json`. Start debugging by opening the debug mode tab and clicking the green debug button.
![debug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4m3moxvtqpnv9gaol0vv.png)
You will now see several options in the window, via which you can pick what logs Xdebugger will show like:

* Notices
* Warnings
* Errors
* Exceptions
* Everything

Access the Debug Console, which displays error information and debug execution buttons at the top.

You have the option to make your selections. What I'm going to tell you now is something I've previously addressed in the early stage breakpoints. A breakpoint on a line of code is now available. It's important to note that Xdebug will display the PHP debug script name and line number in the bottom left area.

In order to debug the application correctly you may launch it in the browser and then go through the code line-by line to spot the issues and fix them. A few shortcut keys for functions and lines of codes should also be remembered:


`F5`:  Continue Debugging

`F10`: Step Over

`F11`: Step into

`Shift + F11`: Step out
![debug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqf89plvc6dtplkvt4pp.png)
## Conclusion
Software development is never complete without debugging.
 You can't tell what's wrong with the code without it.
 A PHP debugger that identifies problems and warning signals fast is a fantastic idea if you're working on an application with thousands of lines of code and you want to be sure you've set up your debugging environment before you start writing code.


In this article, you learnt how to enable PHP debugging with Xdebug and setup it in VSCode. IDEs like as Eclipse, PHPstorm, and others can also be used to incorporate it.