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
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”.
After you’ve finished installing, you’ll need to enable and configure Xdebug. To locate it, run the following command. On Mac:
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:
- Set breakpoints in your code where you’d like the execution to be paused
- Start listening / run a debugging session
- At breakpoints, execute line-by-line and monitor your application.
- End the debugging session.
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?