When you develop web applications you must opt a best practice throughout the development process. One of the best practices in application development is debugging. Debugging has many purposes. I’m not telling you the purpose debugging in application development. Because, You know it’s purpose and that’s why you ended up here. So, in this article we’re going to discuss how to debug PHP with VsCode using Xdebug then,…Debug our code.

What is Xdebug

Xdebug is an extension of PHP which is used to debug your code. Xdebug makes your code development smooth and easy. It allows you to stop the code execution at exceptions or at any place you want by placing breakpoints.

Install Xdebug

Windows

Here is the interesting part. Xdebug provides a precompiled .dll modules for windows users. Just copy and paste the full output of phpinfo() and Xdebug will choose the version that fits you php version and other configurations.

To do that create a test.php file and paste the following code inside it.

<?php
  phpinfo();
  ?>

Or simply open cmd and type php -i and click enter.

Then, copy the output of either the phpinfo() or the php -i and paste them in the Xdebug wizard tool and click on Analyze my phpinfo() output button.

Xdebug installation wizard
paste the result of php -i to the wizard
Xdebug installation instruction
After clicking analyse

Now, download the .dll file and follow the instructions one by one.

First, put the downloaded .dll file in the  C:\php\ext directory.

Next, open C:\Program Files\PHP\php.ini and add the following line then restart your webserever:

zend_extension = C:\php\ext\php_xdebug-2.9.6-7.4-vc15-x86_64.dll

Linux and MAC

You can install on Linux and MAC by using PECL

pecl install xdebug

Then add the extension path to your php.ini file.

zend_extension="/usr/local/php/modules/xdebug.so"
;Don't forget to change the directory to yours

Upon successful completion of all of the above configuration, we’ve successfully installed Xdebug on our machine. If you want to check you have installed it correctly copy the result of phpinfo() and paste it in the wizard Then you the following message

You’re already running the latest Xdebug version

Xdebug installation wizard successful result

How To Debug PHP with VsCode

This is the goal that we want to succeed. In order to succeed this, first we need to setup Xdebug with VsCode.

Setup Xdebug with VsCode

Step 1:

Go to extension market place and search for PHP Debug then click install the PHP Debug (Felix Becker) extension.

PHP debug extension

Reload VSCode window after installation.

Then open your php.ini file and paste the following right after the zend_extension

zend_extension =php_xdebug-2.9.6-7.4-vc15-x86_64.dll
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

Then restart your web server.

Step 2:

From VsCode tabs click Run and click Add configuration. And paste the following code.

{
    
    "version": "0.2.0",
    "configurations": [
        {
            "type": "php",
            "request": "launch",
            "name": "Listen for XDebug",
            "port":9000,
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "port": 9000
        }
    ]
}

Finally, click CTRL+SHIFT+D (to open VsCode debug window) Or simply click the debug icon from left side of your VsCode window. Then click run(play icon).

Debugging using PHP Debug

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *