What Xdebug can do to help you to become a better WordPress developer
Like any professional can confirm that code never is prepared for release following the initial draft. The most important part of developing is debugging -- removing or replacing all the parts of your code that do not perform as expected.
The Xdebug extension available for PHP is one of the most popular ways to find and eliminate any bugs that exist within your PHP code.
One of the best aspects of Xdebug is how flexible it is. No matter what framework or development tool there's a chance to locate a version of Xdebug that fits in with your work flow. From there the process of learning about this tool shouldn't be a problem.
This article will cover Xdebug in depth, including how to install it, integrating it into your configuration and the overall usage.
First, let's give you an understanding of the meaning of Xdebug is and what it does.
The Xdebug project is now live.
Xdebug is among the most popular extensions to analyze you PHP code. Install it within your chosen environment, and it functions as an "step debugger."
This allows you to work on your program line-by-line, so that you are able to step through the code and examine the way that your code works and interacts with your program in addition to analyzing the output. From there, you can modify the code as you think fit.
Xdebug can do much more However:
- When you execute PHP Unit Tests, you will be able to see the code suites you run and execute.
- Xdebug incorporates "tracing" capabilitiesthat will write every function call on disk. It will also record variables, arguments, and return value.
- Xdebug can also improve the traditional PHP error reporting. We'll cover more on this later.
Based on the capabilities There are a variety of methods to employ Xdebug (and any similar debugger) within your workflow. These will be discussed in the following section.
What's the reason you'd want to use Xdebug
Many developers won't utilize a specific debugging procedure which makes use of third-party tools and extensions. It's because PHP comes with its own basic error log. It is possible to use command lines like error_log
, var_dump
, and print to see the output of variables and function calls.
There are, for instance, numerous snippets that you can repurpose for WordPress development. The Stack Overflow community is filled with them:
function log_me($message)
if ( WP_DEBUG === true )
if ( is_array($message)
But, there are crucial disadvantages with this strategy:
- You first must ensure you allow error logs for your platform that you're working with. For this you'll need to enable
the WP_DEBUG feature
(more on this in the near future). - This particular example of "dump" debugging provides the same scope of investigation as step-debugging. Here, you can only output the information you specify.
A tool like Xdebug may work its magic to provide an even greater range of options:
- You can "break" the code throughout the execution process, allowing you to view what is happening in real-time.
- There are a myriad of measurements, visualisations branches, metrics, and much more to help you ascertain the code's behavior and the way it reacts.
- Sometimes, you can even modify values as you go when debugging. This offers immense value when it comes to software that works well. The program can be used to perform manually unit tests at any point.
- Since breakpoints are used to mark up areas to debug and debug, you do not have to work with snippets within the code. It makes your code more efficient and minimizes the chance of issues that could arise in the future.
In the end, using a program like Xdebug can be a active decision, not an impulsive one. Step debugging can be used in the main development procedure, similar to implementing unit tests as part of test-driven development (TDD).
What is the best way to turn on PHP Error Logging
To report every error that occurs, you'll have add the following line at the top of the relevant PHP file:
error_reporting(E_ALL);
This command is catch-all that can accomplish similar results by using the ini_set
function:
ini_set('error_reporting', E_ALL);
It allows you to change the settings inside the php.ini file on an individual basis for each project. While you could go into the file to make a manual change but it's a much better decision to utilize the ini_set
to change the specific setting:
ini_set('display_errors', '1');
When you've got the active error reporting setting at your preference, you can begin working with Xdebug.
What is Xdebug? Xdebug
In the coming parts, we'll explain how to utilize Xdebug, including the steps required to setup the system. While we can't cover every tool aspect, this quick-start guide will get you going fast.
The first step is to need to install the Xdebug program. We'll show you how you can do this.
1. Install Xdebug on Your Operating System (OS)
As Xdebug is able to adapt to any number of setups, the procedure for each one may differ slightly. At the OS scale, there are few distinctions:
- Windows: This is a tad complicated set-up process that involves using an existing PHP file, and the installation wizard, then download the correct version for your system.
- Linux Here, the method could be considered to be the most simple method: You could use a software package administrator in order to set up Xdebug or the PHP Extension Community Library (PECL).
- Apple: This method is also simple: Once PECL is installed and execute
pecl install xdebug
via a Terminal instance. You'll also need to have XCode command-line tools and PHP installed on your computer.
But, the majority of users will not prefer only a single instance of Xdebug. It is better to incorporate it into your own development environment.
2. Integrate Xdebug Into Your Development Environment
When you've installed Xdebug to your OS, you should connect it to your environment.
Variable Vagrant Vagrants (VVV)
VVV is one of the environments named that can be found on the Make WordPress site:
The good news is that VVV already comes with an variant of Xdebug however, you need to activate it. You can do this using Secure Shell (SSH) within the Terminal window
vagrant ssh --c "switch_php_debugmod the xdebug"
It's not too much of a performance hit, though it's necessary to turn it back on if you provision your sites.
Laravel Valet
Some users find that the Laravel Valet provides a perfect Web development environment. Even better, you can use Xdebug to integrate it with.
In order to do this, you'll need to create a configuration file for the debugger. It is possible to find your personal location by typing the command "php-ini"
in the command prompt, which can return several different locations for the file:
Create a new xdebug.ini file at the address of the other .ini files. In our example, it's at /opt/homebrew/etc/php/7.4/conf.d.
After opening this brand new file, open the path to the loaded Configuration File (your primary php.ini file). Once both are open, add the following to the bottom:
- php.ini:
zend_extension="xdebug.so"
- xdebug.ini:
xdebug.mode=debug
When you've saved your modifications After saving your changes, you can run valet restart
from the Terminal Then, add the phpinfo() exit
into the site's file. It is important to verify that this works through a quick loading of the page in your browser.
You may have to restart PHP using sudo brew service restart php
as well as check whether your installation of Xdebug is correct with grep"xdebug"
. You'll notice the Xdebug-specific lines inside the output.
You can try to integrate Xdebug into the editor you prefer to use for coding.
XAMPP
Like Valet like Valet, there are few parts to the process that are used for XAMPP. However, Windows and macOS versions use two distinct procedures.
Install XAMPP and then perform quick checks to check whether the php_xdebug.dll file (Windows) or xdebug.so file (macOS) is installed on your computer:
If the file is present it's time to move to the configuration. In the event that it doesn't, you'll have to download either the right file to Windows which is a 64-bit version for the preferred PHP version or install additional dependencies for the Mac.
output_buffering = Off
At the [XDebugIn the [XDebug
section, add the following three lines:
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=trigger
When you have saved the changes, you can then restart Apache and test to see if Xdebug is working.
To use for Mac You'll need to make sure you have installed the Xcode command-line tools by using xcode select --install
within an Terminal instance. Following that, there are three packages you'll want to install via Homebrew.
Brew install autoconf automake libtool
In certain situations it is necessary to install XAMPP to get both the program's core and its "Developer Files." You should be able to install only these files, but you'll want to carry out the backup of your previous setup prior to doing so.
Next, navigate to the download for the source folder for Xdebug on your system and unpack this TGZ file. Inside a Terminal window, go to this directory, and then run these commands:
PHPize PECL install xdebug
You may have to utilize sudo
to access this feature. From here, you can alter your XAMPP php.ini file. For most macOS installations, you'll find it at /Applications/XAMPP/xamppfiles/etc/php.ini. Inside this directory, you'll also locate the path to your xdebug.so file -- note this down and put it in the place of the placeholder file path for this snippet:
Do you want to know how we increased our volume by more than 1000 percent?
Join 20,000+ others who receive our weekly newsletter that contains insider WordPress tips!
[xdebug]
zend_extension=/path/to/xdebug.so
xdebug.mode=develop,degug
xdebug.start_with_request=yes
To determine if this is working, create a new xdebug_info.php file within the primary Htdocs XAMPP directory. Inside, add the following:
...then then, refresh Apache and then try Xdebug in the browser.
Utilising PhpStorm Using Xdebug
However, many developers opt to use JetBrains PhpStorm. In fact, PhpStorm offers "WordPress-aware assistance" -- and is a favorite choice due to various other reasons too.
This JetBrains website includes full instructions on joining Xdebug and PhpStorm, but we'll review them here.
First, navigate to the Frameworks and Languages and PHP page inside the Preferences pane. This will open the Additional Items Kebab menu, which is next to the CLI Interpreter dropdown menu:
The following will provide additional specifics about the details of your PHP version as well as the interpreter. If you choose the More items ellipsis next to the configuration file option, you'll be able to see full paths for the php.ini file:
You'll be working with this PHP file next to continue the setup process.
The Work is done within the php.ini File
First, you need to delete the lines that influence the way Xdebug works using PhpStorm.
Within the php.ini file, look for the following lines and either remove them or comment them out:
zend_extension=
zend_extension=
The lines may not be visible always So don't worry if you aren't seeing them.
Add the following information to the document:
[xdebug]
zend_extension="xdebug.so"
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=""
There are some things to keep in mind about this set of software:
- There may be an
[xdebugsection. You may already have an [xdebug
section. In that situation you could omit first name. - The
extension zend
entry could require you to enter the complete path of xdebug.so to connect. - Though it could appear as an empty placeholder but it's actually a placeholder.
xdebug.client_port
parameter is what you'll use to define it in the code.
When you've added them add-ons, save and close the file. You can then check the PHP version by using the command line (using php --version
):
If you have a working version of Xdebug and it is installed, it will be displayed in the list of PHP extensions. Additionally, you can add the PHPinfo();
to an existing file and try this in the browser.
That's the only thing you'll need in order to make Xdebug the default debugger for PhpStorm. The final step before using the debugger is to install an extension for your browser that helps you use it.
Installation of a browser helper extension
The final connection you'll require is between your browser and PhpStorm which is accomplished through setting up step debugging on the server. You could accomplish this from on the command line using special POST
or POST
value, it's far simpler to make the connection using an extension.
We suggest using the Xdebug Helper extension. Install it in your preferred browser:
If you want to explore additional extensions such as the JetBrains website has a number of other alternatives for the top browsers.
Once you've installed your chosen browser extension, you won't have to adjust any further configuration settings. After that, you'll be able to begin to use Xdebug using PhpStorm.
Making use of Xdebug
While we'll use PhpStorm in this instance, you'll notice a similar layout and interface between various IDEs -- though there'll be obvious distinctions.
There are some concepts that combine to form the complete experience of debugging
- Breakpoints These are the points that Xdebug stops and allow you to examine the output. You can set any number of them you'd like.
- Listening for connections It is possible to turn this on and off, though many developers prefer to keep it turned off.
- The screen for debugging: Most of your time is located in the debugging screen that's the place you'll be working with various lines of code including variables, parameters, and variables.
The first step is to activate listening -- you can't debug anything without it. For this, click on the Run button and then Begin Listening PHP Debugging Connections option on the toolbar.
If you prefer, you can click on"telephone," or click on the "telephone" icon within the toolbar of PhpStorm:
One of these choices can start listening for connections.
From this point, you may start setting breakpoints in the code editor's gutters. Red dots signify a breakpoint, which you may click to activate.
If you're looking to investigate your program The most simple approach is to get started with listening, then set breakpoints and follow the webpage within your browser. Locate the icon for your extension in the browser. Click on it. Select"Debug" "Debug" alternative:
It will then open the debugger inside PhpStorm and will give either positive or negative news:
If you select the right-click icon for different values, attributes, variables and parameters in the context menu, you'll be able open a contextual menu. This provides you with plenty of extra scope to test and debug the code you write:
For example, you could assign different values to variables along the path. This might be a deliberate attempt to break the code to see what happens, or it could be a way to check your code for bugs that require an update. Whatever the reason, it gives the user a great method of testing your code, without needing alter the code first.
How Helps You to Debug Your WordPress Website?
Command Cheat Sheet
As we close this article, it is important to be sure to mention the ways to cut corners.
Similar to other pieces of software it is possible to use Xdebug (and PhpStorm) with the use of a keyboard. In fact, you could make use of the command line to debug PHP scripts.
When Xdebug is installed and running, use the following commands to get around:
Command | Shortcut |
---|---|
Specific the port to listen to (such like [9003] )
|
|
Creates a breakpoint at the line specified for the path to the file specified. |
|
Your script runs until the end, or your next breakpoint |
|
Steps into the next line that can be executed |
|
This list of variables and values is in the current scope |
|
The value is displayed for the specified property |
|
While your specific code editor will have its own dedicated shortcuts, the focus here is on PhpStorm. Check out this list of keyboard shortcuts that you can use to access Xdebug and PhpStorm.
Command | Windows | macOS |
---|---|---|
Find Action | Ctrl + Shift + A | Shift + Cmd + A |
Open the Debugger | Shift + F9 | Ctrl + D |
Toggle Breakpoint | Control + F8 | Cmd + F8 |
Step into | F7 | F7 |
Step Over | F8 | F8 |
View Breakpoints | Ctrl + Shift + F8 | Shift + Cmd + F8 |
Restart the program | F9 | F9 |
Examine the Present Expression | Alt + F8 | Choice + F8 |
Thankfully, there isn't a lot to memorize in this article. The debugger must be opened and set breakpoints for each line, and listen for connections and then run your scripts.
However, if you need a shortcut for a particular process, then you can utilize this PhpStorm Search Command command:
Once you begin to type within this area, you'll be shown an ever-changing listing of commands as well as related shortcuts. There's also the PDF versions of all keyboard shortcuts in the Help > Keyboard Shortcuts pdf menu.
If you want more of the ability to see shortcuts in real-time when you use the mouse, JetBrains has Key Promoter X, a plugin that allows you to create and manage shortcuts. Key Promoter X software:
The tool displays the latest notifications for your step, as well as the associated keyboard shortcut. When you've learned and are able to utilize the shortcuts, you'll be able to phase the plugin out , and bring back that precious area to your screen.
Summary
Debugging is an art that has come a long way since its inception. It now covers a greater scope than what its predecessors could have possibly had. For a complete task of fix your PHP code, you'll have an effective tool. There are many superb extensions and programs to pick from, but Xdebug is a definite top contender.
Like we've observed, Xdebug can adapt to even the most varied of tastes in code editors, and it's particularly great in conjunction with PhpStorm. However, regardless of your configuration, there'll usually be a variant of the Xdebug that is suitable for your preferences. On the whole, it's a powerful, flexible, and intuitive instrument to utilize.
Are you of the opinion that Xdebug merits the praise it receives, or do you have a different debugging tool that you prefer? Please let us know in the comment section below!
Cut down on time, expenses and improve site performance by:
- 24/7 help and support from WordPress experts in hosting, 24 hours a day.
- Cloudflare Enterprise integration.
- Global audience reach with 35 data centers around the world.
- Optimization using our built-in Application to monitor performance.