Debugging PHP Projects with XDebug
XDebug is a powerful tool for debugging, performance profiling, and code coverage analysis in PHP development. Integrating and using XDebug within the ServBay local development environment can greatly enhance the efficiency and quality of your PHP projects. With XDebug, developers can set breakpoints in their code, inspect variable values, trace function call stacks, analyze performance bottlenecks, and more—making it easier to quickly locate and resolve issues.
Introduction to XDebug
XDebug is an extension designed for PHP that provides advanced debugging and analysis capabilities. It supports remote debugging (typically via the DBGp protocol), breakpoint setting, step execution, variable inspection, function call stack tracing, script performance profiling, and generation of code coverage reports. With XDebug, developers gain deep insight into code execution flow and state, helping them pinpoint and fix problems more effectively.
XDebug supports several modes, but the most commonly used is the debug
mode for interactive breakpoint debugging.
Enabling and Configuring XDebug in ServBay
ServBay comes with the XDebug extension pre-installed for every supported PHP version; manual downloading or compiling is not necessary.
Enable the XDebug Module: Open the ServBay application interface. Navigate to
Languages
-PHP
, and locate the PHP version you wish to use with XDebug. UnderExtensions
, find thexdebug
module and ensure it is enabled. If it isn't, click to enable it. After activation, you may need to restart PHP-FPM (ServBay typically does this automatically).Configure Your IDE: Within your Integrated Development Environment (IDE) such as PHPStorm or VS Code, configure the XDebug connection. This usually involves setting the listen port (to match the port shown in the ServBay interface) and, if necessary, remote path mapping (in ServBay's local environment, this is typically not required).
More IDE configuration details
For step-by-step instructions on configuring XDebug in specific IDEs (such as PHPStorm, VS Code, etc.), refer to your IDE’s documentation or see How to enable the Xdebug module included with ServBay.
Hands-On Example: Debugging a PHP Project in ServBay
Let’s walk through a simple example project to demonstrate how to use XDebug for debugging within ServBay.
1. Set Up a ServBay Website
First, create a new website in ServBay to host our sample project:
- In the ServBay web root directory
/Applications/ServBay/www/
, create a new project folder, for example,servbay-xdebug-app
. - Open the ServBay application interface and go to the "Websites" section.
- Click to add a new website and set its root directory to
/Applications/ServBay/www/servbay-xdebug-app
. - Set a local domain name, such as
servbay-xdebug-app.servbay.demo
. - Select your preferred PHP version and ensure that XDebug has been enabled and configured as described earlier.
- Save and apply the changes. ServBay will automatically configure Caddy/Nginx and update your hosts file (or use ServBay's DNS service).
2. Example Project Structure and Code
Inside /Applications/ServBay/www/servbay-xdebug-app/
, create the following files and directories:
servbay-xdebug-app/
├── src/
│ └── Calculator.php
└── index.php
2
3
4
The content of src/Calculator.php
is as follows:
<?php
namespace App;
class Calculator
{
public function add($a, $b)
{
// Set a breakpoint on this line
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
16
The content of index.php
is as follows:
<?php
// Assuming you are using Composer, require the autoloader here
// If you're not using Composer, adjust the load method as needed
require __DIR__ . '/vendor/autoload.php';
use App\Calculator;
echo "Debugging Example:\n";
$calculator = new Calculator();
$num1 = 5;
$num2 = 3;
$sum = $calculator->add($num1, $num2);
$difference = $calculator->subtract($num1, $num2);
echo "Sum: " . $sum . "\n";
echo "Difference: " . $difference . "\n";
echo "Done.\n";
?>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Note: The above line require __DIR__ . '/vendor/autoload.php';
assumes you are using Composer. If you are simply testing, you can remove this line and replace use App\Calculator;
with require __DIR__ . '/src/Calculator.php';
.
3. Set Breakpoints
In your IDE (for example, PHPStorm), open /Applications/ServBay/www/servbay-xdebug-app/src/Calculator.php
. Click next to the line number of the return $a + $b;
line in the add
method to set a breakpoint.
4. Start a Debugging Session
- In your IDE, enable listening for XDebug connections. In PHPStorm, this is typically done by clicking the "Start Listening for PHP Debug Connections" button in the top toolbar (an icon resembling a phone or bug).
- In your browser, access the
index.php
file on your ServBay website:https://servbay-xdebug-app.servbay.demo/index.php
. - If
xdebug.start_with_request
is set totrigger
, make sure to activate an XDebug trigger by browser extension or manually (for example, by visitinghttps://servbay-xdebug-app.servbay.demo/index.php?XDEBUG_TRIGGER=1
).
5. Debugging Process
- When the browser accesses
index.php
and triggers an XDebug session, XDebug will automatically connect to your IDE and pause execution at the breakpoint you set. - Your IDE will switch to debug view, showing code paused at the breakpoint in the
add
method withinCalculator.php
.
6. Inspect Variable Values
- In the IDE’s debug window, you can view the current code line, call stack, variables, and more.
- In the "Variables" panel, you can inspect the values within the current scope. For instance, you should see
$a
equal to5
and$b
equal to3
.
7. Step Execution
- Use your IDE’s step execution buttons (usually
Step Over
(F8),Step Into
(F7),Step Out
(Shift+F8), etc.).Step Over
: Executes the current line; if it’s a function call, runs the function and jumps to the next line.Step Into
: If on a function call, enters the function's first line.Step Out
: Runs remaining code in current function, then returns to caller context.
- By stepping through the code, you can monitor each line’s execution and watch how variable values change.
8. Resume Program Execution
- Click the “Resume Program” button in your IDE (often a green play icon or F9); the script will continue running to the next breakpoint or completion.
9. View Output
After program execution completes, check the output in your browser (or in the terminal for CLI debugging). For this example, you should see:
textDebugging Example: Sum: 8 Difference: 2 Done.
1
2
3
4
Notes and Reminders
- Firewall: Make sure your OS firewall isn’t blocking the port your IDE is listening on (default is 9003).
- Port Conflicts: Ensure that your configured XDebug port isn’t being used by another application.
- Performance Impact: When
xdebug.mode=debug
andxdebug.start_with_request=yes
, every PHP request attempts to start a debugging session. This can significantly affect site performance. Disable the XDebug module or setxdebug.start_with_request
totrigger
when not actively debugging. - Debugging CLI scripts: XDebug can also be used to debug PHP CLI scripts. You’ll need to set environment variables or use specific parameters to trigger debugging. Refer to XDebug’s official documentation for guidance.
- IDE Versions and Configuration: Different IDE versions and config panels may vary. Please refer to your IDE’s official documentation for details.
Frequently Asked Questions (FAQ)
Q: My IDE cannot connect to XDebug. What should I do?
A: Please check the following:
- Ensure the XDebug module is enabled for the relevant PHP version in ServBay.
- Check that the
php.ini
settings forxdebug.mode
,xdebug.client_host
, andxdebug.client_port
are correct, and thatclient_host
andclient_port
match your IDE’s listener configuration. - If you are using
xdebug.start_with_request = trigger
, confirm that you’ve correctly activated the trigger (GET/POST parameter, cookie, or header). - Confirm your OS firewall allows traffic on the XDebug port.
- Make sure your IDE is listening on the appropriate port.
Q: Why does my website slow down significantly after enabling XDebug?
A: This is likely because you’ve set xdebug.start_with_request
to yes
. In this mode, every PHP request attempts to start a debug session, which has a significant overhead. Set it to trigger
to only enable debugging when needed.
Q: Can I use XDebug to debug AJAX requests?
A: Yes. Debugging AJAX requests is similar to regular HTTP requests—you just need to ensure the AJAX request includes the XDebug trigger (usually via cookie or header).
Q: Besides PHPStorm, does ServBay’s XDebug support VS Code or other IDEs?
A: Yes. The XDebug bundled with ServBay is a standard PHP extension that supports any IDE or editor compliant with the DBGp debug protocol—including, but not limited to, VS Code (with PHP Debug extension), NetBeans, Eclipse, etc. The setup process is similar; you primarily need to set the correct listen port in your IDE.
Conclusion
With XDebug integrated into ServBay, developers can seamlessly and efficiently debug PHP projects in their local environment. Mastering core debugging techniques such as breakpoints, variable inspection, and step execution will dramatically improve your ability to identify and resolve issues, ensuring high code quality. Combined with ServBay’s convenient environment management, XDebug becomes an invaluable asset to your PHP development workflow. Start using XDebug in ServBay and experience smoother, more productive PHP development!