Xdebug is an extension for PHP, and provides a range of features to improve the PHP development experience. Xdebug is a powerful debugging and profiling tool for PHP. It provides features like stack traces, function traces, profiling information, and code coverage analysis. With Xdebug, developers can debug their PHP code more efficiently by allowing them to set breakpoints, step through code execution, inspect variables, and analyze the flow of their applications.
Core features
Step Debugging
A way to step through your code in your IDE or editor while the script is executing.
Error reporting
An improved var_dump()
function, stack traces for Notices, Warnings, Errors and Exceptions to highlight the code path to the error
Tracing
Writes every function call, with arguments and invocation location to disk. Optionally also includes every variable assignment and return value for each function.
Profiling
Allows you, with the help of visualisation tools, to analyse the performance of your PHP application and find bottlenecks.
A Step-by-Step Guide to Installing Xdebug
Check your PHP Version
Make sure you have a compatible version of PHP installed on your system. Xdebug supports various PHP versions, so ensure compatibility before proceeding.
Installation
To install Xdebug, visit https://xdebug.org/docs/install and follow the instructions provided on the Xdebug website to choose the appropriate version for your system. The website offers detailed installation guides for various operating systems and PHP configurations, ensuring you get the right version tailored to your setup.
sudo apt-get install php8.1-xdebug
Enable Xdebug
Once Xdebug is installed, enable Xdebug in your PHP configuration by adding the necessary configuration settings to your php.ini
file.
Add the following line:
zend_extension=xdebug
Restart PHP
After making changes to the php.ini
file, it’s necessary to restart your web server (such as Apache or Nginx) for the changes to take effect.
sudo service apache2 restart
Verify installation
Open Terminal and run the following command:
php -v
Output:
PHP 8.1.2-1ubuntu2.17 (cli) (built: May 1 2024 10:10:07) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.17, Copyright (c), by Zend Technologies
with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
or check if the Xdebug PHP extension is loaded.
php -m | grep xdebug
Xdebug info
Call Xdebug info from the script. Create a PHP file and call xdebug_info(); function. The function will provide details about the installed Xdebug extension, its version, configuration settings, and more.
xdebug.php
<? xdebug_info();
Open your web browser and enter the following text into the address bar.
http://localhost/xdebug.php
Install PHP debug VS Code extension
If you plan to use Xdebug for remote debugging, configure your Integrated Development Environment (IDE). You can use the PHP Debug extension for Visual Studio Code. This extension is a debug adapter between VS Code and Xdebug.
Set breakpoints in the php file and press F5.
Launch currently open script.