跳至主要内容

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.

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

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;
}
}

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";

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

  1. In PHPStorm, click the Start Listening for PHP Debug Connections button (the little bug icon) on the top toolbar.
  2. In the browser, access your PHP application, such as https://servbay-xdebug-app.test/index.php.

Debugging Process

  1. When the browser accesses index.php, XDebug will automatically connect to PHPStorm and pause execution at the set breakpoint.
  2. 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

  1. In PHPStorm’s debug window, you can see the current executing code line, call stack, variable values, etc.
  2. In the Variables panel, you can see the values of the variables $a and $b are 5 and 3, respectively.

Step Execution

  1. Click the Step Over button (or press F8) to step through the code line by line.
  2. Observe the changes in variable values to ensure the add method returns the correct result.

Resume Execution

  1. Click the Resume Program button (or press F9) to continue executing the code.
  2. The program will continue running until it hits the next breakpoint or finishes execution.

View Output

  1. Check the output in the browser, which should display:
Sum: 8
Difference: 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.

评论

此博客中的热门博文

Modern PHP Development in 2024

In 2024, PHP remains a strong contender in modern web development. Despite a decline in its ranking on the TIOBE index, PHP is still one of the most widely used programming languages for websites. Its practicality, efficiency, and performance improvements make it a solid choice. For instance, the latest versions of PHP (like PHP 8.1 and above) have shown significant speed enhancements, making it competitive with Python or Node.js, and even faster in some scenarios. PHP’s development speed is also quite rapid, similar to Python, and it boasts a rich set of built-in functions and libraries that facilitate quick development. Additionally, PHP is relatively easy to deploy, especially for large-scale applications. Applications handling millions of requests can efficiently achieve load balancing with proper endpoint caching in PHP. The advantages and development trends of PHP are evident in several areas. Firstly, PHP is favored for its simple and readable syntax, making it particularly suit...

Must-have for slacking off! 2024 Efficient Dev Tools for Increasing Productivity

  Being a programmer is challenging with the constant flow of code and demands. Balancing work and relaxation is a tough task, but there are powerful tools to ease the workload. “The right tools for the job” — This article aims to help you discover some handy tools. DevDocs — Your Offline Documentation Companion DevDocs  , an offline API documentation browser, supports multilingual, offering developers a quick and efficient way to access tech docs. From front-end to back-end and mobile development, it integrates official documentation, providing a sleek, user-friendly interface. ServBay — A Developer’s Dream ServBay  is hailed as a must-have tool for web developers, providing an instant, all-in-one web development environment. It eliminates the hassle of setting up PHP, Nodejs, Redis, and databases, allowing developers to focus on coding. It includes a range of commonly used web development service software and tools, covering web servers, databases, programming languages...

Best MAMP Alternatives in 2025

  2025 is coming. Are you still using MAMP? There are many local  web development  tools available online, but MAMP is often regarded as a leading choice among developers and freelancers for building and testing websites prior to going live. However, despite its popularity, MAMP does have its limitations. Developers are continually on the lookout for a more efficient  localhost  solution that not only enhances their local web development experience but also simplifies the transition to live environments and fosters better collaboration within teams. If you’re in search of an alternative to MAMP, you’ve come to the right place. I’ve put together a list of the top MAMP alternatives that offer similar functionalities, which you can utilize for your local site development. Before diving deeper, let’s first examine the limitations of MAMP. To start, MAMP provides only  a limited number of PHP versions . For developers who are used to testing their websites acros...