DEV Community

ServBay
ServBay

Posted on • Updated on

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.

Image description

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.

Download: click here to download ServBay

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
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

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 the add method of the Calculator.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 are 5 and 3, respectively.

Step Execution

Click the Step Over button (or press F8) 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 press F9) 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
Enter fullscreen mode Exit fullscreen mode

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.


Big thanks for sticking with ServBay. Your support means the world to us 💙. Got questions or need a hand? Our tech support team is just a shout away. Here's to making web development fun and fabulous! 🥳
If you want to get the latest information, follow X(Twitter) and Facebook.
If you have any questions, our staff would be pleased to help, just join our Discord community

Top comments (0)