When any programming code generates unexpected output, it requires to find out the reason behind the error of the output to solve the problem. Debugging is the best way to find out the reason for the unexpected output of the code by tracing the code step by step.

Normally, print_r() and var_dump() functions are used to check the output of the variables. Xdebug extension is used in PHP for an advanced level of debugging. This extension is not installed in PHP by default. You have to install it and set up the necessary configurations to use its features. How the Xdebug extension can be installed and configured in PHP and integrated with the Visual Studio Code editor on Ubuntu is shown in this tutorial.

Install Xdebug for PHP

Run the following command from the terminal to install the Xdebug extension for the installed PHP version 7 .

$ sudo apt install php-xdebug

You will get the following output if the extension is installed properly in the system.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_1.jpg" data-lazy- height="459" src="data:image/svg xml,” width=”1110″>

Configure the Xdebug extension

Run the following command to create the configuration file named xdebug.ini. You have to set the path of the mods-available folder properly based on your installed PHP version. According to the path used in the command, PHP version 7.4 is installed in the current system.

$ sudo nano /etc/php/7.4/mods-available/xdebug.ini

Add the following lines in the file to set the necessary configurations to enable the Xdebug extension for PHP.

xdebug.remote_autostart = 1


xdebug.remote_enable = 1


xdebug.remote_handler = dbgp


xdebug.remote_host = 127.0.0.1


xdebug.remote_log = /tmp/xdebug_remote.log


xdebug.remote_mode = req


xdebug.remote_port = 9005 #this can be modified

Restart the webserver

Run the following command with root privilege to restart the Apache server.

$ sudo service apache2 restart

Verifying the Xdebug Installation

Run the following command to check if the Xdebug is installed properly or not. If the extension is installed properly, then the following command will show the information of the installed Xdebug version with PHP version.

The following output shows that Xdebug version 2.9.6 is installed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_2.jpg" data-lazy- height="254" src="data:image/svg xml,” width=”1113″>

Configure Xdebug in VSCode

If the Visual Studio code editor is opened before installing the Xdebug extension, then re-open it. Open the Extensions window by clicking the View menu or pressing Ctrl Shift x. Type “PHP debug” in the search box of the Extensions window. You will get the following information if the Xdebug extension is integrated properly with this editor.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_3.jpg" data-lazy- height="669" src="data:image/svg xml,” width=”1195″>

Open the configuration file of Xdebug by clicking ‘Add Configuration…’ from the menu Run and set the necessary configuration according to the xdebug.ini file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_4.jpg" data-lazy- height="669" src="data:image/svg xml,” width=”1198″>

Debugging PHP script using Xdebug

Create a PHP file with the following script to show the way of using Xdebug debugger.

<?php

//Define a site type

$sitetype = “Search Engine”;

//Define the site types

$site1 = ‘yahoo.com’;

$site2 = ‘hi5.com’;

$site3 = ‘aliexpress.com’;

//Check the value of the $sitetype variable

switch($sitetype)

{


    //Define array based on the match and search site in the array


    case ‘Search Engine’:


        $array = Array (‘google.com’,‘ask.com’,‘bing.com’);


        if(in_array($site1,$array))


            echo $site1 is a search engine site.”;


        else


            echo $site1 does not exist in the list.”;


        break;


    case ‘Social Network’:


        $array = Array (‘facebook.com’,‘twitter.com’,‘snapchat.com’);


        if(in_array($site2,$array))


            echo $site2 is a social networking site.”;


        else


            echo $site2 does not exist in the list.”;


        break;


    case ‘E-commerce’:


        $array = Array (‘aliexpress.com’,‘ebay.com’,‘daraz.com’);


        if(in_array($site1,$array))


            echo $site3 is a e-commerce site.”;


        else


            echo $site3 does not exist in the list.”;


        break;


    default:


        echo “Unknown site.”;

}

?>

Select the line in the script and press F9 or click Toggle Breakpoint from the Run menu to set the breakpoint in the script; multiple breakpoints can be set. Click on the Run button from the left side of the editor to see the details of the debugging information. In the following output, two breakpoints are set in line 5 and line 13.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_5.jpg" data-lazy- height="575" src="data:image/svg xml,” width=”1199″>

Now, press F5 or click on Start Debugging from the Run menu to start the debugging by using Xdebug. A toolbar will appear, like the following image for the debugging, and the script will stop its execution at the first breakpoint that is in line 5. In the left window, which variables are initialized and not initialized after executing the script at line 5 is shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_6-3.jpg" data-lazy- height="570" src="data:image/svg xml,” width=”1193″>

The first icon of the toolbar is used to continue the script and to go to the next breakpoint (F5 can be used for this purpose). If F5 is pressed, after starting the debug, it will go to the next breakpoint that is in line 13. At this line, some variables will be initialized.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_7.jpg" data-lazy- height="572" src="data:image/svg xml,” width=”1197″>

The second icon is used to step over the script and to go to the line according to the script (F10 can be used for this purpose).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_8.jpg" data-lazy- height="572" src="data:image/svg xml,” width=”1199″>

The third icon is used to step into the breakpoint line (F11 can be used for this purpose).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/How-to-install-Xdebug-and-use-it-in-PHP-on-Ubuntu_9.jpg" data-lazy- height="569" src="data:image/svg xml,” width=”1197″>

The fourth icon is used to step out from all breakpoint lines (Shift F11 can be used for this purpose). The fifth icon is used to restart the debugging (Ctrl Shift F5 can be used for this purpose). The sixth icon is used to stop the debugging (Shift F5 can be used for this purpose).

Conclusion

Xdebug is a useful extension of PHP used for debugging the script deeply. It helps the coder to find the reason for the complicated errors of the script easily. The way of using this extension is different for the different editors. How this extension for PHP can be installed and configured for the Visual Studio editor on Ubuntu has been explained here. The use of this extension for debugging is also shown using a simple PHP script. This tutorial will help the readers who want to learn the advanced level of debugging for PHP script using Xdebug extension and VSCode editor.

About the author

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/01/echo/channel-logo-150×150.jpg6003c2b5a3ba2.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.