Debugging PHP with Xdebug

Configure Debugging

You have to install the Xdebug package, via apt or pecl.

apt-get install libedit-dev php5-dev php5-xdebug
pecl install xdebug

Edit the Xdebug PHP Configuration, it will be one of the following files, depending on your distribution.

cat /etc/php5/conf.d/xdebug.ini /etc/php/apache2-php5.5/ext-active/xdebug.ini /etc/php/cli-php5.5/ext-active/xdebug.ini



Debugger CLI from Source

Make sure you download the source version that matches the Xdebug that your system has.

cd /usr/src
tar -zxf xdebug-2.2.3.tgz
cd xdebug-2.2.3
make install
cd ./debugclient
./configure --prefix=/opt/xdebug --with-libedit
make install

Debugger CLI from Git

git clone /usr/src/xdebug
cd /usr/src/xdebug/debugclient
./configure --prefix=/opt/xdebug --with-libedit

Breakpoints in Code

Similar to the breakpoints for JS.


How to use Xdebug to debug and profile your PHP applications


With Xdebug profiling enabled it's a simple process to collect detailed information on application performance.

This configuration example below sets the profiler to only work with the proper trigger is set. This way we can run the profiler on a production system, just not for every request. This keeps the load on the system a bit lighter.

xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1
xdebug.profiler_append = 0
xdebug.profiler_output_dir = /tmp/phprof
xdebug.profiler_output_name = xdebug.%R.out

Enable Profiling

To enable profiling the input parameter XDEBUG_PROFILE must be set via GET, POST or COOKIE; any value is acceptable (we generally use "1"). To start profiling, try appending the query string like.

curl -v http://test.domain.tld/?XDEBUG_PROFILE=1

It's possible for your PHP scripts to set this parameter, not for the calling script but for the next request. The example below sets a session cooke COOKIE when the GET parameter is seen.

if (!empty($_GET['XDEBUG_PROFILE'])) setcookie('XDEBUG_PROFILE','1');

Or it's possible to debug only some requests, perhaps some AJAX portions, assuming this jQuery.



Xdebug also comes with tools to improve the error logging and reporting of PHP. Here are some annotated settings for the debugger and trace files to assist with debugging PHP applications. The collected traces will contain lots of detail on the causes of the errors.

Show the parameters to functions in the trace
We disable cause it makes huge spew
Disabled cause its very slow
xdebug.auto_trace = 0
xdebug.collect_params = 4
xdebug.collect_return = 1
xdebug.dump.COOKIE = *
xdebug.dump.GET = *
xdebug.dump.POST = *
xdebug.dump_globals = 0
xdebug.extended_info = 0
xdebug.overload_var_dump = 0
xdebug.show_exception_trace = 1
xdebug.trace_enable_trigger = 1
xdebug.trace_output_dir = /tmp/phprof/xtrace
xdebug.trace_output_name = %R.out

git clone

See Also