What Xdebug can help you to become a better WordPress developer

Oct 13, 2022
Xdebug

Just like any professional, you can be sure that your code has never been prepared for release following the first draft. The most important part of development is debuggingby removing or replacing elements of your code which don't perform as planned.

The Xdebug extension available for PHP is among the most widely used methods to eliminate problems that may be present in your PHP program.

One of the most appealing aspects of Xdebug's flexibility is how adaptable it can be. No matter what framework or development tool there's an opportunity to find a version of Xdebug which is compatible with your work flow. From there the process of getting familiar with this tool shouldn't be an issue.

This article will go over Xdebug in depth, including how to install it, integrating it into your setup as well as the general usage.

In the beginning, let's provide an understanding of the meaning of Xdebug is and what it can do.

The Xdebug project has been launched.

The Xdebug extension is among the extensions most used to examine your PHP code. It can be installed in your preferred environmentand then it acts as a "step debugger."

A green layered background showing the Xdebug logo, complete with a green 'X'.
The Xdebug logo.

This lets you develop your program line-by-line, so that you are capable of stepping through the code and examine the way that your code works and interacts with the programmer, as well as analyzing your output. From there, you can change the code however you think fit.

Xdebug will do a lot more. It can also:

  • When you execute PHP Unit Tests, you can see the scripts that which you test and then execute.
  • Xdebug includes "tracing" capabilitiesthat will write each function's call to disk. It will also record variables, arguments and return value.
  • Xdebug will also help improve traditional PHP error report. We'll discuss this more further down the line.

Based on the capabilities is a myriad of ways to use the Xdebug (and other similar debuggers) in your work flow. They will be described in the following section.

What's the reason you'd want to make use of Xdebug?

Most developers do not employ the same debugging process that makes use of extension tools or tools developed by third party companies. It's because PHP is a program that has its own error log that is basic to use. It is possible to use command lines like error_log, var_dump as well as print to view what happens to variables as well as function calls.

There are, for instance, numerous snippets that you could reuse to help with WordPress development. The Stack Overflow community is brimming with them:

function log_me($message) when ( WP_DEBUG >= true ) in the event that ( is_array($message)

However, there are some major drawbacks of this method:

  • You first must ensure you enable error logs on the platform you're using. To do this, you'll have to turn on the feature WP_DEBUG (more details on this later in the near future).
  • This specific example that uses "dump" debugging provides the same scope of investigation as step-debugging. Here, you can only print the data you've specified.

A tool such as Xdebug can work to give you a greater range of options:

  • You can "break" your code throughout the entire process, allowing you to view what is going on in real time.
  • There is a variety of measures, visualisations of branches as well as metrics and more that can help you understand how code behaves and manner in which it behaves.
  • There are times when you're able to change values during debugging. This can be extremely beneficial in the case of applications that function well. This program is able for unit testing manually at any point.
  • As breakpoints mark up areas to debug and test, you do not have to work with fragments of code. They make your code more efficient and minimizes the chance of issues that may arise in the future.

In the end, using a program such as Xdebug can be a active choice, and not an unintentional one. It is possible to use step debugging to aid in development like implementing unit tests as part of testing-driven development (TDD).

What is the best way to turn on PHP Error Logging

For each error to be reported when it occurs, it is necessary to include the following lines at the top of the appropriate PHP file:

error_reporting(E_ALL);

This command is catch-all that could achieve similar results employing ini_set: ini_set function:

ini_set('error_reporting', E_ALL);

This allows you to alter the settings in the php.ini file on an individual basis for each project. You can go to the file to modify the settings manually, it's better to make use of the ini_set for changing the exact setting:

ini_set('display_errors', '1');

If you've set the active error reporting setting at the setting you prefer, you're able to start working using Xdebug.

What exactly is Xdebug? Xdebug

In the coming sections, we'll show you the process of using Xdebug, including the steps required to setup the software. Although we're not able to cover every tool aspect, this quick-start guide will get you going fast.

First, you'll have for the installation of Xdebug program. The following steps will help you will be able to install it.

1. Install Xdebug on your Operating System (OS)

Since Xdebug can be adapted to any number of setups, the process for each one may differ slightly. On the OS level, there are few distinctions:

  • Windows: This is an a bit complicated setup process , which requires an existing PHP file, and the installer wizard and then downloading the appropriate version for your system.
  • Linux Here, it could be thought to be the most simple way to go: Use the program administrator software to set up Xdebug or PHP Extension Community Library (PECL). PHP Extension Community Library (PECL).
  • Apple: This method is also simple: Once PECL is installed , you can execute the pecl installation xdebug through a terminal instance. Also, you'll need XCode command-line tool and PHP to be installed on the system.

The majority of users will not prefer only one Xdebug instance. It's better to integrate it into your own development environment.

2. Integrate Xdebug Into Your Development Environment

When you've installed Xdebug to your OS it is recommended that you connect it to your environment.

Variable Vagrant Vagrants (VVV)

VVV is one of the environments named available in the Make WordPress site:

A blue background containing 8-bit ASCII art of the Varying Vagrant Vagrants logo (
The logo that is part of that of the Varying Vagrant Vagrants logo.

The good news is that VVV already comes with an version of Xdebug however, you have to enable it. You can do this using Secure Shell (SSH) within the Terminal window.

vagrant sshh -c "switch_php_debugmod the xdebug"

It's not too much in terms of performance, though it's necessary to switch it off when you've provisioned your websites.

Laravel Valet

Many users feel that it is the Laravel Valet provides a ideal Web development platform. Additionally, you could use Xdebug to integrate it with.

For this, you'll need to build a configuration file for the debugger. It's possible to locate your private location by typing "php-ini" or "php-ini" in the command prompt. This will return several different locations for your file.

A Terminal window showing a list of paths of .ini PHP configuration files, and an empty command prompt cursor.
The Terminal shows a list of paths for configuration files.

Create a brand new xdebug.ini file at the same address as the various .ini files. In our example, it's at /opt/homebrew/etc/php/7.4/conf.d.

When you open this brand-new file, navigate to the loaded Configuration File (your principal php.ini file). Once both are open Add the following information at the bottom of the file:

  • php.ini: zend_extension="xdebug.so"
  • xdebug.ini: xdebug.mode=debug

After you've saved the modifications Once you've saved your modifications, you can run valet restart in the terminal. Then, insert the PHPinfo() exit in the website's files. You must confirm that this works through a quick loading of the page on your web browser.

A PHP information file within the browser, showing the Xdebug logo and the
This screen is known as this screen. PHP info screen.

There is a possibility of restarting PHP with sudo brew restart php in addition to determining whether your installation of Xdebug is correct with grep"xdebug". You'll notice the Xdebug-specific lines inside the output.

A Terminal window showing a number of different Xdebug configuration settings (such as output names and display variables). The prompt shows the

It is possible to incorporate Xdebug to the editor you would like to use for programming.

XAMPP

Like Valet like Valet There are a few elements that can be used to create XAMPP. The difference is that Windows and macOS versions utilize two distinct procedures.

Install XAMPP and perform quick checks to check whether the php_xdebug.dll file (Windows) or xdebug.so file (macOS) is installed on your system:

A Terminal window showing a snippet to test whether a while exists. If it does, the Terminal will output
A terminal window that displays the test results for an XAMPP file.

If the file exists, it's time to move to the settings. If the file isn't there, you'll need to download the correct file for Windows that is an updated 64-bit version for your preferred PHP version, or install any more dependencies to Mac. Mac.

output_buffering = Off

On the [XDebugIn the"XDebug" section, include these three lines

zend_extension=xdebug xdebug.mode=debug xdebug.start_with_request=trigger

After you've saved your modifications, then restart Apache and check to see the if Xdebug works.

For use on Mac It is necessary to ensure that you've downloaded the software Xcode command-line tools by using the xcode option --install inside a Terminal instance. After that, there are three programs you'll need to install through Homebrew.

Brew install autoconf make libtool automake

Certain situations make it required to be able to install XAMPP in order to access the core of the program as well as it's "Developer files." It is possible to install only these files, but you'll need to make the backup of your current installation prior to doing this.

Next, navigate to the source download folder of Xdebug on your computer and open this TGZ files. Within the Terminal window, open the directory and run these commands:

PHPize PECL install xdebug

You may have to utilize sudo to gain access to this function. You can change your XAMPP php.ini document. For most macOS installations, you'll find it at /Applications/XAMPP/xamppfiles/etc/php.ini. In this directory, you'll locate the path to the xdebug.so file -Note this down, and place it in the location of the placeholder file address for this code snippet:

Are you interested in knowing how we increased our volume by more than 1000 percent?

Join the 20,000+ who are receiving our weekly newsletter which contains tips from the inside. WordPress advice!

[xdebug] zend_extension=/path/to/xdebug.so xdebug.mode=develop,degug xdebug.start_with_request=yes

To determine if this is working, create the fresh xdebug_info.php file within the primary Htdocs XAMPP directory. In the file, include the following details:

...then the next step is to refresh Apache then, test Xdebug on the browser.

Utilising PhpStorm Using Xdebug

But, many developers prefer to make use of JetBrains PhpStorm. In fact, PhpStorm offers "WordPress-aware help" -- and is the most popular choice due to various other reasons too.

The PhpStorm interface, with a tree directory for the project on the left that uses white, blue, and yellow text. The right-hand side contains PHP code for a WordPress plugin, using orange, purple, green, and white text.
It's it's PhpStorm interface.

The JetBrains web site provides complete instructions for joining Xdebug and PhpStorm however we'll go over them here.

Then, go to first the Frameworks, Languages in the Frameworks and Languages PHP page within the Preferences pane. This will display the Additional Items Kebab menu, which is next after the CLI Interpreter dropdown menu:

A partial PhpStorm Preferences screen, showing the page link (
Picking an interpreter for CLI within PhpStorm.

This article will give additional specifics about the details of your PHP version, as well as the interpreter. If you select the More items in the ellipsis after the configuration file choice, you'll be able to see full paths to this php.ini file:

A partial PhpStorm More Items screen showing the name of the configuration, a path to the PHP executable file, the current PHP and debugger version numbers, and a list of configuration files for various aspects of the PHP installation.
Modifying the PHP configuration inside PhpStorm.

It will be your turn to work with this PHP file next to continue the setup process.

The Work is done in the php.ini File

In the first place, it is necessary remove the lines that influence how Xdebug works using PhpStorm.

In within the php.ini file, look for the following lines and either remove these lines or make comments on them:

zend_extension= zend_extension=

They may not always be apparent, so don't worry if you aren't being able to see the lines.

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 be aware of with the software in this collection:

  • There may be an [xdebugsection]. There may be an [xdebug ] section. If so, you can omit first name.
  • The extension zend entry could need you to input the full path to xdebug.so to connect.
  • It may look like an empty placeholder, it's really it's a placeholder. xdebug.client_port parameter is what you'll be using to identify it in your code.

If you've made the necessary extensions, save and close the file. Then, you can verify the PHP version by using the command line (using php --version):

The Big Sur blue, red, and purple gradient desktop background, showing a macOS Terminal window. It shows the "php -version" command, along with the PHP version number, a copyright notice, and installed versions (complete with version numbers) for Zend Engine, Xdebug, and Zend OPcache.
Testing for the current installations of PHP and Xdebug by using macOS Terminal. macOS Terminal.

If you've got a working version of Xdebug and it's installed, it will be displayed as a part of the listing of PHP extensions. You can also include the PHPinfo(); to the file you have already installed and test this in the browser.

That's the only thing you'll need in order to enable Xdebug to be the default debugger for PhpStorm. The final step before using the debugger is to download an extension to your web browser to help you utilize the debugger.

Installing a browser assister extension

The connection that's required will be between your web browser and PhpStorm. This is accomplished through creating step debugging in the server. This can be done by using your command line using special POST and POST value, but it's much more simple to connect by using an extension.

We would recommend installing our Xdebug Helper extension. Install it in your preferred browser:

If you are looking to try more extensions like the JetBrains website has a number of other alternatives for the leading browsers.

After you've installed the chosen browser extension, it won't have to adjust any further options for configuration. Once you've done that, you'll able to begin to use Xdebug with PhpStorm.

Utilizing Xdebug

While we'll use PhpStorm in this case You'll see a similar layout and interface among the various IDEs -- though there'll have obvious differences.

Certain concepts combine to form the complete understanding of troubleshooting

  • Breakpoints These are the locations that Xdebug stops and allow you to review the output. You are able to define any number of them you'd like.
  • listening for connection You can switch this off and on, though many developers prefer to keep it turned off.
  • The screen that you use to debug: Most of your time will be spent on the screen for debugging, which is where you'll work on various lines of code including parameters, variables, and variables.

The first thing to do is start listening. This is because you are unable to debug any issue without it. To do this, press the Run button after that, select the Begin Listening to PHP Debugging Connections option on the toolbar.

A partial PhpStorm interface screen, showing a tree directory on the left, and the application toolbar. The Run menu is open, and the
It is the Open Run menu within PhpStorm.

If you prefer to use a phone, select on"telephone," or click on the "telephone" icon in the toolbar of PhpStorm:

A close-up of the PhpStorm toolbar, which displays options for the current run configuration, various Git options, and the Start Listening for PHP Debug Connections telephone icon (complete with tooltip).
Keep an eye out for PHP connection debugging via the telephone icon on the toolbar for PhpStorm.

A single option can be to begin listening for connections.

At this point it is possible to set breakpoints within the editor's gutters. Red dots signify a breakpoint that you can select to enable.

A close-up of the PhpStorm interface, showing a tree directory on the left, and the coding panel on the right. Within the gutters of the editor, there are red dots with white ticks at lines 45, 50, and 55. These are breakpoints for debugging.
Breakpoints in the middle of an editing page.

If you're looking to investigate your application, the easiest way to begin is with listening, then set breakpoints and follow the webpage in your browser. Look for the icon that represents your extension in the browser. Hit it. Select"Debug" "Debug" alternative:

The Mozilla Firefox browser, showing the green background (and
You can select the Debug option in the toolbar in your browser using an extension specifically designed for it.

Then, it will open the debugger in PhpStorm and give positive or negative information:

A partial PhpStorm screen, showing an open debugger panel. The left shows various breakpoints, complete with filenames, line numbers, and function references. The right shows the values of the variables throughout the code, along with the value types.
Making use of the PhpStorm debugger with the Xdebug.

If you select the right-click icon to select different values, attributes, variables and parameters in the context menu, you'll be able to open a contextual menu. This gives you plenty of extra scope to test and debug the code you write:

A portion of the PhpStorm Debugger interface with a list of breakpoints, filenames, line numbers, and function references on the left. The right shows a highlighted value within the code, with a context menu open. Among the options is "Set Value…" — the way to assign new values to variables and debug the code.
Utilizing the context menu for right-clicks within the PhpStorm Debugger to set new numbers.

For example, you could assign various values to variables along the line. It could be an intentional attempt to break the code to see what happens, or it could be a way to examine the code for any bugs which require an update. No matter what the case this gives users the opportunity to evaluate your code, without needing modify the code in advance.

What can you do to help you debug Your WordPress Website?

The My dashboard, showing the left-hand purple sidebar, and a further gray submenu. There are two options to toggle here — a search and replace tool on the right, and the WordPress debugging tool on the left.
The WordPress Debugging function is accessible via My dashboard. My dashboard.
The Dev dashboard for a single site. It shows the database settings, including host, port, name, and user credentials. There is also a
It is possible to activate WP_DEBUG can be found in Control panel of the Dev.

Cheat Sheet for Command

When we conclude this piece It is crucial to not forget to discuss the ways to cut corners.

Similar to other pieces of software it is possible to run Xdebug (and PhpStorm) with the use of keyboards. Actually, you can utilize the command line to test PHP scripts.

When Xdebug is up and running you can use these instructions to help you:

Command Shortcut
The specific port that you want that you want to hear (such as [9003]) -p [value]
Creates a breakpoint at the location specified that is the path to the specified file. breakpoint_set -t line file:/// -n
The script will run until final breakpoint run
Steps into the next line of action that is able to be completed step_into
The following list of value and variables are in the current scope context_get
The value is displayed for the specified property property_get -n

Although your particular code editor is likely to have its own special shortcuts, this article is on PhpStorm. Take a look at this checklist of keyboard shortcuts which you can utilize to get access to Xdebug as well as PhpStorm.

Command Windows macOS
Find Action Ctrl + Shift + A Shift + Cmd + A
Unlock the Debugger Shift + F9 Ctrl + D
Toggle Breakpoint Control + F8 Cmd + F8
Step inside F7 F7
Step Over F8 F8
View Breakpoints Ctrl + Shift + F8 Shift + Cmd + F8
Start the program again. F9 F9
Review the present expression Alt + F8 Option + F8

There's not a lot to memorize in this post. The debugger must be opened and breakpoints set for each line, and listen for connections and then execute your scripts.

But, if you're looking for an alternative to a certain procedure, you could make use of the PhpStorm Search Command command:

The PhpStorm interface, showing the Find Action display. There are various search filters for All, Classes, Files, Symbols, Actions, and Git. The search term is
Utilizing PhpStorm's Find Action menu within PhpStorm.

Once you begin to type within this area You'll see the ever-changing list of commands, as well as associated shortcuts. Also, you can find pdf versions of each keyboard shortcuts within the Help menu, which is located under the Keyboard Shortcuts PDF menu.

If you're interested in the ability to see shortcuts in real-time using the mouse, JetBrains has Key Promoter X is a plug-in that lets you design and manage shortcuts. Key Promoter X software:

A PhpStorm interface window, showing a number of options to find files and projects alongside the corresponding shortcuts. There are two notifications in the bottom right-hand corner with the format of
The PhpStorm interface shows Key Promoter X notification messages.

The tool displays the latest notifications for your step, as well as the associated keyboard shortcut. When you've learned and can use the shortcuts, you'll be able to gradually remove the application and restore that precious display.

Summary

Debugging is a skill that has come a long way since its inception. It now covers a greater extent than its predecessors could have. For a complete task of fix the errors in your PHP codes, it's best to have an extremely efficient program. There are a variety of excellent extensions and software to choose from, but the Xdebug extension is a definite leading contender.

We've noticed that Xdebug can adapt to even the most varied of tastes when it comes to code editors. it's especially great when combined together with PhpStorm. But, whatever your settings, there's always a variant of the Xdebug that's suitable for your preferences. Overall it's an effective user-friendly, versatile and well-designed instrument that you can use.

Are you of the opinion that Xdebug deserves its praise Do you use an alternative debugging software you would prefer? Let us know by commenting by commenting below!

Cut down on time, expenses and improve the performance of your site by:

  • 24/7 help and support from WordPress experts in hosting all hours of the day.
  • Cloudflare Enterprise integration.
  • Global audience reach with 35 data centers around the world.
  • Optimization using our built-in Application to monitor the performance.

Article was posted on here