PHP Debugging and Profiling – Xdebug

Date: 06th May 2013 Author: admin 2 Comments
Posted in: PHP |Tags: , ,

PHP Debugging and Profiling - Xdebug

PHP Debugging and Profiling – Xdebug

When we are talking about web development, PHP surely one of the most popular languages in the world. Its popularity can be judged from the fact that all the major CMS solutions available are powered by PHP. However for long PHP developers faced a problem in absence of a suitable debugger which other programming languages such as Java and C# enjoyed. Overcoming this meant manually adding debug statements which was quite a challenging task. This problem was solved once Xdebug hit the scenes and soon became one of the most popular PHP extensions.



Xdebug

Xdebug is a free and open source and offers PHP developers much more than just basic debugging support. It can be used for stack traces, profiling, code coverage etc. In this write-up we shall take a look at how we can install and configure Xdebug. Apart from this we shall also look at how we can debug and read a profiling report.

Installation

Installing Xdebug is extremely easy as it is available as PECL extension. In our example we shall be using debian/Ubuntu. This process may be slightly different for other Linux distributions. To install a PECL extension we need to install PEAR first.

To install a PECL extension you need to install PEAR first. Be sure that you have installed PEAR and continue then with installing the Xdebug PECL extension:

Install PECL/PEAR for phpShell

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

Install Xdebug as PECL extensionShell

sudo pecl install Xdebug

Configuration

Once the installation is over you need to your php.ini and add the following line to the extension section:

php.ini

zend_extension=/usr/lib/php5/{BUILDDATE}/xdebug.so

Make sure you use ‘zend_extension’ instead of ‘extension’ and replace {BUILDDATE} with the date you found in your php5 lib folder.

php.ini

xdebug.remote_enable=1 # enable remote debugging for all hosts, which use this php.ini
xdebug.remote_handler=dbgp # debugger protocol, you may change this value to 'gdp' if your IDE is supporting only this
xdebug.remote_mode=req # Xdebug tries to connect to your IDE as soon as you start a script, you can choose 'jit' when Xdebug shall connect to our IDE only when an error condition occurs
xdebug.remote_port=9000 # default port for Xdebug
xdebug.remote_host=aaa.bbb.ccc.ddd # the IP of the remote debugger (your local IP)
xdebug.remote_autostart=0 # Xdebug does only start when you set GET/POST/COOKIE variable, if you set it to '1' Xdebug starts a debugging session on every script call

With these steps you have installed and configured Xdebug with a basic setup.

Debugging

Now that we are done with the installation and configuration let us see how the debugging process works. You might be tempted to use the var_dump() and exit/die() combination for debugging but this process will require you to modify the code for debugging. You can bypass this problem in Xdebug as it lets you pause your application’s execution where ever you want. You can easily configure Netbeans to act as an Xdebug client. Open the options window (Tools > Options) and in the PHP section go to the debugging tab. Enter the debugging port given in php.ini and a Session ID as this allows you to run the debugger by clicking Debug in the tools tab.

Now press the Debug button in the toolbar to start debugging. An application opens in a browser, and PHP’s execution will pause at the first line if you have enabled “Stop at first line” otherwise, it will run until it encounters the first breakpoint. Go to the next break point using the continue button. You will notice XDEBUG_SESSION_START parameter in the browser’s URL. To start the debugger you must pass XDEBUG_SESSION_START as a request parameter (GET/POST) or XDEBUG_SESSION as a cookie parameter. Further you can easily add breakpoints by clicking the line number in the editor’s margin and then pause execution on them.

Profiling

To optimize an application you must profile them. This records important details like the time it takes for statements and functions to execute or the number of times they are called. To make use of Xdebug as a profiling tool for PHP add the following settings to php.ini

xdebug.profiler_enable = 1
xdebug.profiler_output_name = xdebug.out.%t
xdebug.profiler_output_dir = /tmp
xdebug.profiler_enable_trigger = 1

Please note that profiling is disabled by default in Xdebug as it can reduce the performance of PHP. To enable it you will have to use xdebug.profiler_enable. You don’t want to run it all the time and thus make use of xdebug.profiler_enable_trigger to instruct Xdebug to perform profiling only when XDEBUG_PROFILE is passed as a GET or POST parameter.

To sum up Xdebug is one of the most useful extensions for PHP developers. It controls the execution of PHP programs in the server. It also allows you a feature to remote debug and inspects values at runtime, without modifying your program. In the installation process we have discussed how to install Xdebub for remote debugging.

Author: Mark Wilston works with PixelCrayons.com, an India-based outsourcing and consulting firm. PixelCrayons offers extended teams to its clients, helping them to reduce Time To Market (TTM) and enhance Return On Investment (ROI). With services such as CMS website development and eCommerce development, PixelCrayons helps clients define and leverage their offshore strategy, offering them “value for money”. One can hire PHP programmer here for effective PHP development.

Enjoyed this Post?

    Tweet
   
   

Stay connected with us:

If you enjoyed this article, feel free to share our tutorial with your friends.

2 Comments

    • Balaji's Gravatar
    • Will somehow learn PHP faster and understand the above post. Excluding that I love this blog….

    • Arjun's Gravatar

Leave a Reply to Arjun Cancel reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image
Refresh Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>