Use XDebug for PHP Project Debugging
XDebug is an indispensable debugging tool in PHP development, offering powerful features for breakpoint debugging, performance analysis, and code coverage. With XDebug, developers can set breakpoints in the code, inspect variable values, trace function call stacks, analyze performance bottlenecks, and greatly enhance PHP development efficiency and code quality.
XDebug Introduction
XDebug is a PHP extension designed to provide debugging and analysis capabilities. It allows developers to set breakpoints in the code, step through the code, inspect variable values and program states, helping them better understand and debug the code.
Enable Xdebug and Configure Debugging Environment
ServBay comes with XDebug pre-installed for each PHP version.
Note
Please refer to the article How to Enable ServBay's Built-in Xdebug Module for information on how to enable the Xdebug module
and configure PHPStorm
.
Specific Debugging Example
Sample Project Structure
Assuming we have a simple PHP project with the following directory structure:
servbay_xdebug_app/
├── src/
│ └── Calculator.php
└── index.php
2
3
4
The content of the Calculator.php
file is as follows:
<?php
namespace App;
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
public function subtract($a, $b)
{
return $a - $b;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
The content of the index.php
file is as follows:
<?php
require 'vendor/autoload.php';
use App\Calculator;
$calculator = new Calculator();
$sum = $calculator->add(5, 3);
$difference = $calculator->subtract(5, 3);
echo "Sum: " . $sum . "\n";
echo "Difference: " . $difference . "\n";
2
3
4
5
6
7
8
9
10
11
Setting Breakpoints
We want to debug the add
method in the Calculator
class and see how it executes. Open the Calculator.php
file in PHPStorm and set a breakpoint on the line return $a + $b;
.
Starting the Debugging Session
- In PHPStorm, click the
Start Listening for PHP Debug Connections
button (the little bug icon) on the top toolbar. - In the browser, access your PHP application, such as
https://servbay-xdebug-app.test/index.php
.
Debugging Process
- When the browser accesses
index.php
, XDebug will automatically connect to PHPStorm and pause execution at the set breakpoint. - In PHPStorm, you'll see the code paused at the
return $a + $b;
line in theadd
method of theCalculator.php
file.
Inspecting Variable Values
- In PHPStorm's debug window, you can see the current executing code line, call stack, variable values, etc.
- In the
Variables
panel, you can see the values of the variables$a
and$b
are5
and3
, respectively.
Step Execution
- Click the
Step Over
button (or pressF8
) to step through the code line by line. - Observe the changes in variable values to ensure the
add
method returns the correct result.
Resume Execution
- Click the
Resume Program
button (or pressF9
) to continue executing the code. - The program will continue running until it hits the next breakpoint or finishes execution.
View Output
- Check the output in the browser, which should display:
Sum: 8 Difference: 2
1
2
Conclusion
XDebug allows developers to easily set breakpoints, inspect variable values, and step through code in PHP, enabling them to better understand and debug code. In practical development, XDebug's breakpoint debugging feature can help developers quickly locate and resolve issues, improving development efficiency and code quality. Through the specific debugging example above, we can see the powerful features and convenience of XDebug in PHP project debugging.