跳至主要内容

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.

评论

此博客中的热门博文

Software for MacOS Developers that cannot Miss, Again

  Here’s a summary of essential software for macOS development. All of these tools are free, and most are open-source. I hope they enhance your development experience. Basics Git Git needs no introduction. Simply run  git  in the terminal, and a dialog will pop up. Click install. This typically installs the basic Xcode runtime environment as well. Alternatively, you can install it by running  xcode-select --install  in the terminal. ServBay ServBay is probably the best development environment for Mac. It allows easy one-click installation of various development environments and simplifies subsequent upgrades. For teams, it ensures consistency in dependencies and configurations. Terminal Tools iTerm2  +  Oh-My-Zsh iTerm2 is the premier terminal on Mac, and Oh-My-Zsh provides powerful theming and plugin capabilities. Terminus A minimalist, cross-platform shell tool that I often use to connect to cloud servers. Debugging Tools Bruno Since Postman became p...

Beware the Mid-Career Crisis for Programmers: The Four Major Causes

In the rapidly evolving internet industry of today, questions and discussions like “Is 35 a turning point for programmers?”, “Do programmers really face unemployment at 35?”, and “What’s next for programmers after 35?” are rampant. The debate and concern over a so-called “crisis at 35” for programmers have become hot topics. This may stem from an uncertainty about the future and a fear of the pace at which AI technology is developing. As they age, programmers might face the risk of becoming “obsolete” and struggle to adapt to industry changes. “However, the solution lies with the one who tied the bell,” so to speak. We should face and address these issues, explore the reasons behind them, and understand how to avoid such situations. Therefore, this article delves into the root causes of these issues and offers practical advice to help programmers avoid these pitfalls, enhance their professional level and achievements, and stay ahead of the times. Not Proficient with Tools As programmer...

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...