Debugging with Xdebug
Xdebug is a powerful debugging and profiling tool for managing and controlling PHP program source code. It helps programmers to indicate program source code execution, perform program source code performance profiling, and manually set program source code breakpoints, which helps beginner and expert web developers efficiently find and resolve problems during the program development process. Here you can find detailed information on setting up and using the Xdebug framework for source code debugging in PHP programming.

What is the Xdebug framework?
Xdebug is a PHP software extension that provides PHP web developers with program source code debugging and profiling features. It helps web developers debug and manually modify various types of program source code.
Step-by-Step Debugging in PHP Programs.
Running through source code step by step.
- Error breakpointing is the process of stopping program execution at a particular breakpoint in program source code.
- It previews the program stack trace, the call stack of function calls in a program.
- It performs program source code coverage analysis, tracking which parts of the program source code are covered by testing.
- This source code profiling helps analyse your application’s performance.
Setting up the Xdebug extension in PHP.
So let’s learn how a typical user can install and setup the Xdebug PHP extension in their local system development environment.
Step 1 – Install the Xdebug PHP Extension.
To install the Xdebug PHP extension on your computer, follow the steps below according to your current operating system.
Xdebug PHP Extension for Linux Ubuntu.
sudo apt-get install php-xdebug
xdebug for macOS operating systems with Homebrew.
brew install php-xdebug
Xdebug PHP extension for Windows operating systems.
- First, download the latest Windows-supported version of Xdebug from the Xdebug PHP Extension website in your web browser.
- Extract the php_xdebug.dll file here to your PHP extension directory (usually C:\php\ext).
- Manually modify your php.ini file to load the Xdebug PHP extension on your local computer.
- zend_extension=”C:\php\ext\php_xdebug.dll”
Step 2 – Enable the Xdebug extension in php.ini
After installing the Xdebug extension on your local system, you need to enable it in the php.ini file. Typically, modify the php.ini file in /etc/php/7.x/cli/php.ini for Linux, or C:\php\php.ini for Windows.
Add the following lines to the php.ini xdebug file.
[xdebug]
zend_extension=”path/to/xdebug.so” ; Linux or macOS
; OR
zend_extension=”C:\php\ext\php_xdebug.dll” ; Windows
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host = “127.0.0.1”
xdebug.client_port = 9003
xdebug.log = /tmp/xdebug.log
xdebug php.ini xdebug file explanation.
- xdebug.mode = debug – This statement enables debugging mode in xdebug.
- xdebug.start_with_request = yes – This tells Xdebug to start debugging the program source code with each request. You can use triggers here to only start the debugging process when it is explicitly triggered in the specified order.
- xdebug.client_host and xdebug.client_port – Here, specify the IP address and port where your Xdebug debugger should connect. Typically, this displays something like 127.0.0.1 and port 9003.
- xdebug.log – This contains optional settings to indicate the log file for Xdebug messages.
Step 3 – Restart the Xdebug web server (if applicable).
If developers are running the Xdebug extension on an Apache or Nginx web server platform, restart the server after updating php.ini to apply any modifications.
sudo service apache2 restart
# or for the Nginx server platform
sudo service nginx restart
Step 4 – Install an IDE or text editor plugin in your system environment.
The Xdebug PHP extension communicates with your IDE or text editor, allowing developers to control debugging. Popular IDEs and editors that support the Xdebug extension are included.
IDE or text editor plugin choice.
- PHPStorm – This is a popular IDE software for PHP, with built-in Xdebug PHP extension support.
- VS Code – Manually install the “PHP Debug” extension for Visual Code Xdebug support by Microsoft.
- NetBeans – This is another popular IDE software with built-in Xdebug integration, which provides web developers with PHP along with many other software development environments.
For Microsoft Visual Studio VS Code.
Manually install the PHP Debug extension created by Felix Becker for VS Code on your system.
Manually configure the launch.json file to connect Microsoft VS Code to the Xdebug PHP extension.
{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “Listen for Xdebug”,
“type”: “php”,
“request”: “launch”,
“port”: 9003,
“pathMappings”: {
“/var/www/html”: “${workspaceFolder}” // Local path to server’s path
}
}
]
}
Once installed and configured on their computer, users can start listening for Xdebug connections directly from their IDE.
Use Xdebug for debugging PHP programs.
Step 1 – First, set a breakpoint.
After setting up and starting the Xdebug PHP extension on your computer, you can set breakpoints in your existing program source code where developers want to stop their source code execution. The method for setting breakpoints in a program varies depending on the IDE or text editor.
- PHPStorm/IntelliJ – Manually click the gutter next to the line numbers in the left margin to set a breakpoint in the program.
- VS Code – Manually click the gutter next to the line numbers in the program source code to set a breakpoint.
Step 2 – Start a debugging session in the system.
Once the breakpoints are set on your computer, start a debugging session.
In your system IDE, start a debugging session with “Listen for Xdebug” or a similar command.
Access the script in your web browser, or if it’s a CLI script, run it through the CLI.
In this process, when Xdebug hits a breakpoint, program execution will stop, allowing users to analyse and examine the current state of their application. Developers will find several features available.
- Current Stack Trace – This is the call stack of the current program, displaying the request position in the program source code.
- Variable Values - View the current values of all program variables within the program scope.
- Step-by-Step Execution – This allows developers to review their program source code line by line, stepping into, and out.
- Inspect Objects – View the properties and methods of any object in your program source code.
Step 3 – Inspecting Program Source Code or Data.
While debugging program source code, developers can analyse multiple types of data.
- Variables – Preview the values of current program variables.
- Objects – Preview the structure and properties of objects in the current program.
- Arrays – Analyse array data with program values and data types.
In a user IDE, there will typically be panels for the following:
- Locals – These are variables declared locally within the current scope of the program.
- Watches – These are variables in the program that the user specifically wants to track and control.
- Call Stack – This is the call stack in a function, displaying the execution path leading to the current line.
Step 4 – Step through the program source code.
With a breakpoint set and Xdebug running.
- Step Over – Here the current program moves to the next line of source code, excluding program function calls.
- Step Into – Here you dive into the current function or method and see how it works internally.
- Step Out – This exits the current function in the current program and returns to the caller.
Following these steps allows programmers to perform a deep analysis of what is happening line by line in their source code, helping them find bugs or performance-related problems.
Advanced Features of the PHP Xdebug Extension.
Profiler.
The Xdebug extension can profile program source code to preview performance metrics like function call times and memory usage.
To enable profiling, add this to php.ini.
xdebug.mode = debug,profile
xdebug.profiler_output_dir = “/path/to/profiler/output”
After this, it will generate profiling files with Xdebug. These can be analysed with tools like Webgrind or KCachegrind to identify performance-related hurdles.
Trace files in Xdebug.
The Xdebug PHP extension can also generate trace files, which track program function calls and generate a log output. These are useful for understanding the execution method of your program source code and detecting problems like infinite loops or false logic.
To enable tracing in your program, add the following to php.ini.
xdebug.mode = trace
xdebug.trace_output_dir = “/path/to/trace/output”
This process creates trace files that users can analyse manually or with tools.
Debugging in a web environment vs. a CLI.
- In a web environment – The Xdebug extension can be triggered when a webpage is accessed through a web browser. This is more useful for debugging web applications or APIs.
- In CLI scripts – Xdebug can also debug command-line scripts executed through the CLI with a debugging session. To start debugging from the command line, use the php -d option, or configure your IDE to trigger debugging for CLI scripts.
Key benefits of using Xdebug in web development.
- It provides a step-by-step debugging process to track your program source code execution.
- It also provides breakpoint features to stop program execution at crucial moments.
- It provides variable analysis features to check program values and data types.
- The xdebug extension provides profiler features to identify program performance-related hurdles.
- xdebug helps you explore files for detailed function call analysis.
Conclusion of Debugging with Xdebug in PHP.
The Xdebug PHP extension is a unique and important tool for web developers. With its ability to step through program source code, analyse variables, and profile performance, it significantly improves the debugging process. Whether web developers are debugging a simple script or a complex application, the Xdebug PHP extension helps users identify what is happening behind the scenes, allowing developers to more efficiently correct problems.

