Catalog / Xdebug Cheatsheet
Xdebug Cheatsheet
A comprehensive cheat sheet for Xdebug, covering installation, configuration, debugging features, and common use cases to streamline PHP debugging.
Installation and Configuration
Installation
|
Basic Configuration
|
Enables the Xdebug extension. |
|
Sets the Xdebug mode to ‘debug’ for debugging features. Can also use ‘profile’, ‘coverage’, ‘develop’, ‘coverage,debug’. |
|
Automatically starts a debugging session for every request. |
|
Specifies the host where the debugging client (IDE) is running. |
|
Specifies the port on which Xdebug attempts to connect to the debugging client. |
|
Specifies the file where Xdebug logs its activities. Useful for troubleshooting. |
Configuration Verification
|
Debugging with Xdebug
IDE Integration
Xdebug integrates with various IDEs like VS Code, PhpStorm, and NetBeans. Configure your IDE to listen for Xdebug connections on the specified port (e.g., 9003). |
VS Code: Install the ‘PHP Debug’ extension and configure the |
Breakpoints
Setting Breakpoints |
Place breakpoints in your code where you want the execution to pause. Use your IDE to set breakpoints by clicking in the editor’s gutter. |
Conditional Breakpoints |
Set breakpoints that trigger only when a specific condition is met. This helps in debugging complex scenarios. |
Line Breakpoints |
Break on specific lines of code to examine variables and execution flow. |
Exception Breakpoints |
Break when specific exception is thrown. |
Debugging Operations
Step Over: Execute the current line and move to the next line in the same scope. |
Watch Variables: Monitor the values of specific variables as you step through the code. This helps track changes and identify issues. |
Advanced Xdebug Features
Profiling
Xdebug can generate profiling information to analyze the performance of your PHP code. Profiling helps identify bottlenecks and optimize slow-running code. |
Enable profiling by setting |
Use tools like KCachegrind or Webgrind to visualize and analyze the profiling data. These tools provide insights into function call durations, memory usage, and other performance metrics. |
Code Coverage Analysis
Enabling Coverage |
Configure Xdebug to collect code coverage data by setting |
Generating Reports |
Use PHPUnit with the |
Analyzing Coverage |
Review the generated reports to identify uncovered code and improve your test suite. |
Remote Debugging
Xdebug supports remote debugging, allowing you to debug code running on a remote server from your local IDE. Configure |
Ensure that the remote server can connect to your local machine on the specified port (e.g., 9003). You may need to configure firewall rules to allow the connection. |
Use SSH tunneling to create a secure connection between your local machine and the remote server. This is especially useful when debugging code in production environments. |
Troubleshooting
Common Issues
Xdebug Not Loading: Verify that the |
Debugging Techniques
Logging |
Use |
Remote Session |
Start a debugging session from your browser by adding |
Error Handling |
Use |
Code Review |
Sometimes, the best debugging technique is to carefully review your code and look for potential errors. Use Xdebug in conjunction with code review to identify and fix issues. |
Xdebug 3 Migration
Xdebug 3 introduces significant changes in configuration. Replace the old |
Use |
Ensure that your IDE is compatible with Xdebug 3 and that you have updated your debugging configuration accordingly. |