Wednesday, December 21, 2011

PHP debug & profiling for dummies

Sometimes very basic debug can be done by dumping the values of variables. The other method is about writing debug values into log files. Such approaches are hard to be successfully used on large sophisticated projects like Drupal or Magento. In such case the best solution is to use the debugger and debug your code via step by step code execution, back traces, breakpoints, variables scope inspection and producing execution flows / profiling dumps.

Here is the short compilation of materials about debug and profiling in PHP where everyone can find links to the interesting materials on the WEB and also my personal experience.


You can look at the following slides to get the information about debug and profiling for PHP in general
http://www.slideshare.net/jignesht/debugging-in-php-presentation

There a lot of options available out there, but I worked with ZendDebugger and xdebug. The benefit of ZendDebugger is that it is shipped with Zend Server and supported by Zend Studio by default. So if you are working completely on Zend's software stack - it can be reasonable for you to use ZendDebugger. Anyway it is closed source software distributed only as compiled binary, so can't be available in Linux repositories out of the box. Also it utilizes its own debugging protocol that has a binary nature in contrast to plain text/XML DBGp protocol used by xdebug. You can find detailed DBGp vs Zend Debugger protocols comparison in article by Derick Rethans.

On other hand, xdebug - is an open source solution, so you can see how it works and customize it for your needs it is available under all distributions, so can be easy installed from official distro repository or PECL. Also it supports standard Unix debugging protocol DBGp, so you can debug your code from under any editor that supports debugging under Linux.
I recommend this debugger and all examples that will be presented in this article are listed in respect to xdebug debugger.

Installation

Debian/Ubuntu users can do:
# sudo apt-get install php5-xdebug

RedHat/CentOS/Fedora users:
# yum install php5-xdebug

Installing from PECL is as easy as:
# pecl install xdebug

# load extention in appropriate xdebug.ini
echo -e "zend_extension=\"/usr/lib64/php/modules/xdebug.so\"\nxdebug.remote_enable = 1" > /etc/php.d/xdebug.ini

# restart Apache
/etc/init.d/httpd restart

To make sure xdebug was successfully installed, just type 

# php -i | grep xdebug
OR
call phpinfo() inside your PHP script and you will see the output of xdebug settings.

Generally you are ready to go with debugging.

Further reading

You can continue reading the following list of articles about debug and profiling in PHP with xdebug:
Why use Xdebug?
PHP debug process internals with Xdebug
Xdebug PHP scipts profiling examples

I've listed only a small piece of available functions provided by xdebug, there are lots of methods and magic tricks out there. Generally using debug & profiling practices will help you greatly increase your performance in investigation of issues/problems you have, and allows better understanding how everything works, especially for projects with huge code base like Drupal or Magento. Enjoy!