What can Xdebug do to help you to become an even better WordPress developer

Oct 14, 2022
Xdebug

Just like any professional, you can be sure that your code hasn't been prepared for release following the first version. One of the most crucial aspects of the development process is debugging, which involves eliminating or replacing parts of your code which don't perform as planned.

The Xdebug extension available for PHP is one of the most popular methods to fix any issues that might be present in your PHP program.

One of the attractive advantages of Xdebug's adaptability is how flexible it is able to be. No matter what tools or frameworks you use, you're using, there's a chance to locate a version of Xdebug that is appropriate for your particular workflow. Once you've found that, learning about this software shouldn't pose a problem.

This post will cover Xdebug in depth, including the installation procedure, integrating it into your system and the overall usage.

In the beginning, let's give a better understanding of what the purpose of Xdebug is, and also what it does.

The Xdebug project has officially launched.

The extension Xdebug is one of the extensions that are most commonly used to analyze the PHP code. The extension can be set up in your environment of choice and serves as a "step to debug."

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 can be competent to walk through the code to examine how the program works and interacts with the programmer, as well as analyzing your output. From there, you can modify the code in any way you like.

Xdebug can do much more. It also can:

  • If you run PHP Unit Tests, you can see the scripts that test, and later begin to execute.
  • It also has "tracing" capabilitiesthat will write each function's call to disk. Additionally, it will record variables, arguments and return value.
  • Xdebug can also aid in improving the traditional PHP error reports. The discussion will continue down the line.

Based on the capabilities is a myriad of ways to use the Xdebug (and other debuggers similar to it) to improve your workflow. These will be explained in the following section.

Why would you want to make use of the Xdebug tool?

Many developers don't use the same debugging process that utilizes extension tools or tools developed by companies that are third-party. The reason is that PHP is a programmer that has an error log of its own that is a standard tool to use. You can use command line options that include the error_log, var_dump as well as print to observe the changes that occur to variables as well as the function call.

There are, for instance several snippets you can reuse in order to aid you with WordPress development. The Stack Overflow community is overflowing with these snippets:

function log_me($message) when ( WP_DEBUG > false ) when ( is_array($message)

There are however some major drawbacks of this method:

  • First, you must have enabled error logs on your platform. For this it's necessary to switch on the feature WP_DEBUG (more information about this in the near future).
  • This particular example which uses "dump" debugging gives you the same scope of investigation like step-debugging. This is because you only print the data you've specified.

A tool such as Xdebug is able to offer an increased variety of possibilities:

  • You can "break" your code through all of the process, allowing the user to observe what's taking place in real-time.
  • There's a range of measures, visualisations of branches, metrics that will assist you to understand how code behaves and manner in which it behaves.
  • Sometimes, you're in a position to alter values when debugging. This is extremely helpful when it comes to programs that work well. This program is able for unit testing manually at any point.
  • Because breakpoints define zones to test and debug it is not necessary to deal with fragments of code. Breakpoints make the code more effective and minimizes the chance of issues that may arise at some point in the future.

In the end, using a software like that of Xdebug is a smart option, certainly not one that is unintentionally made. You can use the step debugging feature to assist in development like implementing unit tests as part of testing-driven development (TDD).

How do I best to turn on PHP Error Logging

In order 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 a catch-all for how you could achieve similar results employing ini_set: ini_set function:

ini_set('error_reporting', E_ALL);

This allows you to alter the settings in your php.ini file on an individual basis for every project. If you want to access the file and modify the settings yourself, however preferring to take advantage the ini_set feature. the set ini setting to alter the precise settings:

ini_set('display_errors', '1');

If you've set your active error reporting settings to the level you like and you're ready to begin working with Xdebug.

What is Xdebug? Xdebug

In the next segments, we'll walk you through the process of using Xdebug, including the steps required to setup the software. While we're unable to cover every tool aspect but this guide to getting started will make it easy to get up and running.

In the beginning, you'll need to complete the installation of Xdebug. These steps will assist you set it up.

1. Install Xdebug onto your Operating System (OS)

Since Xdebug can be adapted to various configurations and configurations, the procedure for each one may be different. At the OS scale, there are few distinctions:

  • Windows: This is a bit difficult procedure to set up, that needs the existing PHP file and an installer wizard and then downloading the right version to your particular system.
  • Linux In this case, it could be thought to be the simplest method to use: Utilize the program administrator program to set up Xdebug and PHP Extension Community Library (PECL). PHP Extension Community Library (PECL).
  • Apple: This method is very simple. Once PECL is installed, you will be able to execute the PECL installation xdebug via a terminal. You'll also require XCode command-line utility and PHP for installation on your system.

The majority of users will not prefer only one Xdebug instance. It is better to incorporate the Xdebug application into your personal development environment.

2. Integrate Xdebug Into Your Development Environment

If you've downloaded Xdebug onto your OS it's suggested that you join it with your current environment.

Variable Vagrant Vagrants (VVV)

VVV is among the named environments available on the Make WordPress website:

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

It's good to know that VVV already comes with an version of Xdebug However, you must enable it. This can be done using Secure Shell (SSH) within the Terminal window.

vagrant sshhh sshhh with a -c "switch_php_debugmod using xdebug"

It's not too much in terms of performance, though it's necessary to turn it off once you've set up your sites.

Laravel Valet

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

For this, you'll need to make an configuration file for the debugger. It is possible to find your private location simply by typing "php-ini" or "php-ini" in the command prompt. It will show you a variety of options for the location of your file.

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

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

Once you have opened this brand-new file, you will be able to navigate to the configured configuration File (your principal php.ini file). After both files are opened, 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 When you've saved the modifications, you can run valet restart within the terminal. After that, you can insert the PHPinfo() exit in the website's files. Make sure that you can verify that it is working by a rapid loading of the site in your browser.

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

It is possible to restarting PHP by using sudo brew restartphp and also checking whether the installed Xdebug version is working with grep"xdebug". There are Xdebug-specific line numbers 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

You can add an Xdebug editor into the one you would like to use to program.

XAMPP

Like Valet similar to Valet There are some components that could be utilized to create XAMPP. The difference is that Windows or macOS versions employ two distinct methods.

Install XAMPP and run quick tests to verify that you have the php_xdebug.dll file (Windows) or xdebug.so file (macOS) is present on your computer:

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

If the file exists, it's time to move to settings. If it's not there it's time in order to download the right version 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 your "XDebug " section of the"XDebug" section, Include the following three lines

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

After you've saved your modifications After saving your changes, restart Apache and verify whether Xdebug is working.

for use for use on Mac It is necessary to make sure that you've downloaded the application Xcode command-line tools by using the xcode command --install in the Terminal instance. After that, there are three applications you'll need install through Homebrew.

Brew installation autoconf make the libtool automake

In certain situations, you need to be able to install XAMPP in order to access the core of the program as well as it's "Developer documents." You can download only the files you need, however you'll need an archive of your existing installation prior to installing it.

Then, go to the folder for downloads from the source of Xdebug on your computer and then open the TGZ files. Within this Terminal window, you can open the directory and run these commands:

PHPize PECL installation xdebug

You might need to run sudo for 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. Within this directory, you'll locate the path to the xdebug.so file -Note it down and put it on the same location as the placeholder file's address. this code snippet:

Are you interested in knowing how we were able to increase our volume by over 1000 percent?

Join over 20,000 people that receive our weekly newsletter , which includes tips from the inside. WordPress advice!

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

In order to determine if the method can be verified, you need to create a fresh xdebug_info.php file within the primary Htdocs XAMPP directory. In the file, include the following information:

...then the next step is to restart Apache then, run Xdebug tests on the browser.

Utilising PhpStorm Using Xdebug

Many developers opt to use PhpStorm from JetBrains. In fact, PhpStorm offers "WordPress-aware help" -- and is the most popular option because of a variety of other factors 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 its PhpStorm interface.

The JetBrains site offers complete directions on how to sign up for Xdebug and PhpStorm but we'll cover these in this article.

After that, you can go to The Frameworks, Languages in the Frameworks and Languages PHP page in the Preferences pane. It will show the Additional Options Kebab menu. It comes next to you have the CLI Interpreter dropdown menu.

A partial PhpStorm Preferences screen, showing the page link (
Selecting an interpreter to use for CLI in PhpStorm.

This post will provide further details regarding the specifications of the details of your PHP version and also the interpreter. If you choose Additional items within the ellipsis following the option to configure the file choice, you'll have the ability to view all path 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.
Editing the PHP configuration within PhpStorm.

The time will come for you to work with this PHP file next to continue the set-up process.

The Work is done within the php.ini File

First first, you must remove the lines that influence how Xdebug is used with PhpStorm.

Inside the php.ini file, look for the following lines, and then either delete these lines or make comments about them:

zend_extension= zend_extension=

It is possible that they aren't always obvious, so don't be concerned when you're unable to recognize the lines.

Include the following details to the document:

[xdebug] zend_extension="xdebug.so" xdebug.mode=debug xdebug.client_host=127.0.0.1 xdebug.client_port=""

There are certain things you should be aware of with the software in this collection:

  • It could be that there is an [xdebugsection]. It could be an [xdebug section. If there is, you are able to eliminate the initial name.
  • The extension Zend entry may require users to enter the complete URL to xdebug.so to connect.
  • While it might appear as an empty placeholder but it's actually just a placeholder. xdebug.client_port parameter is the one you'll use to determine it within your code.

If you've created the required extensions, you should save and then close the file. After that, you'll be able to check the PHP version by using this 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.
Checking the latest versions for PHP and Xdebug by using macOS Terminal. macOS Terminal.

If you've got a working version of Xdebug and have it installed the extension will appear in the listing of PHP extensions. You can also include PHPinfo(); to the extension that you've already installed , and then test it in your browser.

This is the sole thing that you'll need to allow Xdebug to become the default debugger in PhpStorm. Another step prior to using the debugger is downloading an extension to your web browser that will allow you to use the debugger.

Installing a browser assister extension

The connection required for this will be between your web browser as well as PhpStorm. This is accomplished through setting up step debugging within the server. This can be done by using your command line with a special POST and POST value, but it's much more simple to connect using an extension.

We suggest installing the Xdebug Helper extension. Install it in your preferred browser:

If you're trying to try more extensions like the JetBrains site, it provides a range of alternatives to the most popular browsers.

After you've installed the chosen browser extension, it won't require any additional choices for setting. Once that's done it's possible to start to use Xdebug with PhpStorm.

Utilizing Xdebug

While we'll use PhpStorm for this example, you'll have a similar layout and interface across the different IDEs however they'll have obvious differences.

Certain theories are combined to make the complete understanding of troubleshooting

  • Breakpoints These are the locations that Xdebug is stopped and allows to review the output. It is possible to set any number of them you'd prefer.
  • waiting for connection It is possible to turn this off and back on, but some developers would rather keep it turned off.
  • The screen that can be used for debugging: Most of your time is spent at the debugging screen the code, and this is where you'll work on various codes, including parameters, variables, and variables.

The most important thing to do is start taking note of. This is because you are incapable of debugging any issue without listening. To accomplish this, hit the Start button, and after that, select the Start 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
This is it's the Open Run menu within PhpStorm.

If you would prefer using the phone, click on"telephone," or click on the "telephone" icon in the toolbar on 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 troubleshooting via the phone icon in the toolbar for PhpStorm.

There is a single way to listen to connections.

At this point it is possible to create breakpoints in your editor's gutters. Dots in red indicate breakpoints which you are able to activate.

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 on an editor's page.

If you're interested in investigating your application, the easiest approach to start is the application's audio, and then you can make breakpoints and navigate the webpage in your browser. Search for the icon that represents your extension on the browser. Hit it. Select"Debug" "Debug" alternative:

The Mozilla Firefox browser, showing the green background (and
You can select the Debug tab in the toolbar of your browser by using an extension specifically designed to support it.

After that, it'll open the debugger in PhpStorm and provide positive or negative data:

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, you can also use the Xdebug.

If you select the right-click icon to select various values, attributes as well as variables and parameters from the context menu, you'll have the option to launch a contextual menu. This will give 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.
The context menu is used for right-clicks within the PhpStorm Debugger to create new numbers.

In this case, for instance, you might assign various value to variables on the line. This could be an attempt to break your code in order to observe what happens. It can be used to examine the code for any issues that need the updating of your code. No matter what the case it gives the user an opportunity to examine your code without having to modify your code in advance.

What are you able to do to help 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 feature can be accessed 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 in the control panel on the Dev.

Cheat Sheet for Command

When we conclude this piece It is crucial to not forget to discuss how to reduce costs.

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

When Xdebug is up and running follow these steps to help you:

Command Shortcut
The exact port you would like to hear (such as [9003[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 continue to run until the final breakpoint run
Moves on to the next course of action that is able to be accomplished step_into
The following list of value and variables is within the scope of current legislation. context_get
The value is displayed for the specified property property_get -n

While your specific code editor is likely to include its own specific keyboard shortcuts, this post is available on PhpStorm. Look over the following list of keyboard shortcuts that you can use to access Xdebug along with PhpStorm.

Command Windows macOS
Find Act Ctrl + Shift + A Shift + Cmd + A
Lock the Debugger Shift + F9 Ctrl + D
Toggling Breakpoint Control + F8 Cmd + F8
Step into the F7 F7
Step over F8 F8
View Breakpoints Ctrl + Shift + F8 Shift + Cmd + F8
Start the program again. F9 F9
Re-examine the expression in the present Alt + F8 Choice + F8

There's nothing to learn in this blog post. Debuggers must be open and breakpoints set for each line, and wait for connections before you start your scripts.

If you're searching for an alternative to certain procedure, you could make use of this 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.

As you type inside this section, you'll notice the constantly changing listing of commands and the associated shortcuts. Additionally, you will download PDF versions of all keyboard shortcuts within the Help menu. It is found in the Keyboard Shortcuts PDF menu.

If you're looking for the ability to see shortcuts in real-time using the mouse JetBrains has Key Promoter Key Promoter X, a plug-in which lets you design and manage shortcuts. Key Promoter Key Promoter 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 notifications.

The tool displays the latest updates for your actions, as well as the associated keyboard shortcut. Once you've learned how to make use of the shortcuts, then you'll have the ability to slowly eliminate the program and return that precious display.

Summary

Debugging is a technique which has advanced a great way since its inception. The skill now has a wider degree than what its predecessors be able to. To fully correcting the flaws in your PHP codes, it's best to utilize a highly effective program. There's an array of great extensions and applications to choose from, but the Extension Xdebug is a definite leading contender.

We've observed that Xdebug has the ability to adjust to the most diverse of preferences regarding editors of code. It's particularly great when used along with PhpStorm. But, whatever your settings, there's always a variant of Xdebug that's appropriate to your needs. Overall , it's an efficient user-friendly, versatile and well-designed tool that is easy to make use of.

Are you convinced that Xdebug is worthy of praise? Do you have a different debugging software you would prefer? Tell us about it by commenting in the comments below!

Reduce time and expenditures and boost the performance of your site through:

  • Support and assistance 24/7 from WordPress experts on hosting at throughout the day.
  • Cloudflare Enterprise integration.
  • Reaching a global audience with 35 data centers all over the globe.
  • Optimization with our built-in Application to monitor the performance.

This article was originally posted this site

Article was first seen on here