Performance Zone is brought to you in partnership with:

I was born in 1981 in one little city. Since I was 10y/o I programmed in different languages. My first languages were basic, then C++/MFC, after .Net (C#, VB.Net, J#, ASP.Net), XSL+XML processing). In the last 5 years I worked with web languages (HTML, CSS, PHP, SQL, XML, XSL, JavaScript). After university I worked in several different companies, eventually becoming a blogger. This is my hobby too. Andrey is a DZone MVB and is not an employee of DZone and has posted 109 posts at DZone. You can read more from them at their website. View Full User Profile

PHP Debugging and Profiling – Xdebug

05.07.2013
| 2627 views |
  • submit to reddit

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

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.




Published at DZone with permission of Andrey Prikaznov, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)