November 1, 2013 by Roman Ožana

How to Prepare a PHP Development Environment for Debugging

While Testomato focuses on front-end UI, our team knows from personal experience how important it is to be able to find and address issues on the back-end. As developers, we often face challenges with our code. If something goes wrong, it’s often hard for us to figure out what has happened. Debuggers simplify that process by allowing us to figure out why our code is not behaving the way we expect. They also show us how to investigate the way systems work and the problems that may occur within them. Learning to properly debug can help you:

  • Debug complex code
  • Understand and investigate the “ghost in the shell” if you’re using 3rd-party code from frameworks or libraries
  • Catch hard repeatable errors
  • Watch variable values

There a few tools you can use to make the process easier, but today, we’ll be focusing on preparing your PHP development environment for the debugger our team uses: Xdebug.

What is Xdebug?

Xdebug is a PHP extension designed to help you through the process of debugging your applications. What you can use Xdebug to do:

  • stack/execution traces
  • memory allocation
  • code profiling
  • code coverage analysis
  • interactive debugging

Xdebug is an open source project created by Derick Rethans, and it’s available for free.

Enable error reporting

Before you start debugging you need to be sure that Xdebug is enabled. A lot of production systems disable error reporting by default in order to avoid users coming across error code. So, if your PHP script is acting up or crashing without you receiving error messages – you need to turn on error reporting. To enable error messaging, change your display_errors and error_reporting settings in php.ini.

Install Xdebug using PECL

In order to install Xdebug, you’ll need to fulfill some requirements. For this post, we’re going to assume that you already have Apache2 and PHP5 up and running. Open your terminal and type the following commands in order to install Xdebug through PECL:

sudo apt-get install php5-dev php-pear
sudo pecl install xdebug

Once it has been installed, it should state: “install ok”.

Configure Xdebug

After you’ve finished installing, you’ll need to enable and configure Xdebug. To locate it, run the following command. On Mac:

locate xdebug.so

or on Ubuntu:

find / -name 'xdebug.so' 2> /dev/null

Xdebug should be installed as something similar to this: /usr/lib/php5/. Once you’ve found it, remember the path. Now, open the php.ini in your editor. You’ll need to make sure the path there matches the one you just found to the xdebug.so file. If it doesn’t, replace it like we have below:

zend_extension="/usr/lib/php5/20060613/xdebug.so" ; path to xdebug.so file

Next, you’ll need to insert the following lines somewhere at the end of the file:

xdebug.remote_enable=1
xdebug.remote_port="9000" ; or another port if you have this port busy
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/Users/roman/tmp/"
xdebug.idekey=PHPSTORM

Just double check to make sure these lines aren’t already in the php.ini file. Sometimes, the PECL installer can place them automatically.

Restart Your Server

The final step of the install process will be to restart your server to make sure all the changes take effect. Enter the following command:

sudo apachectl restart

Finally, you’ll need to check if Xdebug has loaded correctly. To verify this, you can run the following command:

php --ini | grep xdebug

Or, you can check php.net/manual/en/function.phpinfo.php.

Integrate Xdebug with your IDE (Netbeans or PHPStorm)

Both integrated development environments (IDEs) will run similarly with Xdebug. Execution will be paused at every line that you’ve set a breakpoint. When the program pauses, Xdebug will retrieve information about the current status of the program. This means your debugging workflow will look something like this:

  1. Set breakpoints in your code where you’d like the execution to be paused
  2. Start listening / run a debugging session
  3. At breakpoints, execute line-by-line and monitor your application.
  4. End the debugging session.

For more information about how to configure Xdebug, read: Netbeans – HowToConfigureXdebug JetBrains – Configuring Xdebug  PHPStorm bookmarklets.

A couple more tips…

As we mentioned earlier, make sure the extension you download is compatible with the the version of PHP you’re using. Don’t forget, you should not be using any other Zend extensions, since they can conflict with Xdebug.

Have more questions? 

Don’t hesitate to ask us in the comments below or on Facebook. You can always tweet us directly @testomatocom.

Is your website available? Testomato can check your website availability every 15 seconds from 10 different locations around the world and will send you an alert if the site is unavailable.

Keep track of the important parts of your website with simple checks that can run every minute. They can check plain words, HTML code, HTTP headers, redirects … and much more. Never miss anything.

Websites break, it happens all time. You'll be the first to know. Testomato has an extensive database of and will let you know if it finds any error on monitored url.