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!

5 comments:

  1. Little long but really helpful thing I have found here. I think it will help to many other people.
    php application development

    ReplyDelete
  2. I would be pleased if all WebPages provided such articles.
    Web Development

    ReplyDelete
  3. Such a nice blog, you really provide me some great information. i am going to bookmark this blog, share with my friends and will definitely visit here again. Thanks for your article.
    Jimmy Wilson-Smallville Leather Jacket for Sale

    ReplyDelete
  4. Superb publish I adore the article; enjoy how you described everything, your are performing a great career the majority of other folks just like you by way of of which type of educational blogs supply consciousness to all of us linked to several things. I read another fascinating information sites out of your web sites in addition to I'm a whole lot engaged along with your running a blog abilities, When i likewise begun to generate document and also this form conversation really help us available. We by now book marked the web site in addition to discussed ones internet websites to the acquaintances besides me although every one of them just like your current writing a blog expertise, desire an individual produce a lot more intriguing information sites like this one particular and best of luck on your foreseeable future websites.
    Jimmy Wilson-Different Costumes in Resident Evil 6

    ReplyDelete