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+.
You will get the following output if the extension is installed properly in the system.
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.
Add the following lines in the file to set the necessary configurations to enable the Xdebug extension for PHP.
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.
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.
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.
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.
Debugging PHP script using Xdebug
Create a PHP file with the following script to show the way of using Xdebug debugger.
//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
//Define array based on the match and search site in the array
case 'Search Engine':
$array = Array ('google.com','ask.com','bing.com');
echo "$site1 is a search engine site.";
echo "$site1 does not exist in the list.";
case 'Social Network':
$array = Array ('facebook.com','twitter.com','snapchat.com');
echo "$site2 is a social networking site.";
echo "$site2 does not exist in the list.";
$array = Array ('aliexpress.com','ebay.com','daraz.com');
echo "$site3 is a e-commerce site.";
echo "$site3 does not exist in the list.";
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.
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.
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.
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).
The third icon is used to step into the breakpoint line (F11 can be used for this purpose).
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).
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.