Debugging with PHPStorm and Xdebug

See what's under the hood.

Categories: Development, WordPress

Setup

If you haven’t configured MAMP to use Xdebug yet then you can do so by reading this quick article.

In addition, add the following just under zend_extension.

  xdebug.remote_autostart=1
  xdebug.remote_enable=1
  xdebug.remote_port=9000
  xdebug.remote_host=127.0.0.1
  xdebug.remote_connect_back=1
  xdebug.idekey=PHPSTORM

It should be noted that Xdebug should not be on your production server and enabling the remote_connect_back flag is also not recommended for publicly accessible staging servers also.

By default, PHPStorm will expect to connect to Xdebug on port 9000.

PhpStorm - Server Settings

Confirm that you have Xdebug selected for your server.

PhpStorm - Start Listening for PHP Debugger Connections

Giving it a try.

Next, Enable PHP Debugger connections by clicking on the Phone under Run / Start Listening for PHP Debugger Connections.

And because of our local configuration that will allow for remote enable, remote auto start, we don’t need to go set up any other configurations with PHPStorm.

Now click on the gutter near the line numbers to add a breakpoint, this will tell Xdebug to pause and allow you to step through your code execution.

PhpStorm - Breakpoint

Now if you head over to your browser and reload the project may be prompted to accept the debugger connection.

PhpStorm - Debugger

You will be able to see the debug view, and environmental variables. Granted this is an oversimplified view.

But you get the point.

I personally don’t debug all that much but find it can be useful in WordPress when you might be trying to track down hooks, filters, or actions.

Further reading:


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!