PHP XDebug

PHP XDebug

README

XDebug is a PHP extension, created by Derick Rethans one of the PHP core developers, which will helps you debugging your script by providing a lot of valuable debug information including these features:

  • Stack traces and function traces in error messages with:
  • Full parameter display for user defined functions
  • Function name, file name and line indications
  • Support for member functions
  • Memory allocation
  • Protection for infinite recursions

Installation

In UNIX like systems can be installed using the PHP Extension Community Library known also as PECL, executing this command pecl install xdebug; you can additionally check if your system have in their repositories the php5-xdebug package, if it exists then you can use APT, YUM or whatever you want to install this module and its dependencies more easily; however these options doesn’t work on all systems and probably you’ll need to compile it from the source codes. I personally can use this command to install this module in my BackTrack machine using this command apt-get install php5-xdebug, but I want to configure some options before use it, so that I’ll compile from the sources:

git clone git://github.com/derickr/xdebug.git
cd xdebug
phpize
./configure --enable-xdebug --with-php-config=/path/to/php-config
make
cp modules/xdebug.so /to/wherever/you/want/it
zend_extension="/wherever/you/put/it/xdebug.so"

Activating the html_errors parameter in the file php.ini with this command sed -i 's/html_errors = Off/html_errors = On/g' /php5-configuration-path/php.ini and having restarted your web browser because the above process changed the php.ini file, we can check the output of php -i | grep -i xdebug or php -m to know if the new module was installed correctly.

PHP Var_dump()

Xdebug also provides profiling information for PHP scripts, script execution analysis and capabilities to debug your scripts interactively with a debug client being an excellent addition to PHP developers. XDebug offers interesting alternatives to the extensive use of var_dump() statements for debugging your code; when the XDebug extension is loaded, the output of the PHP’s var_dump( $variable_a, $variable_b, ... ) function is automatically beautified for better readability; the following screen-shots show how change a PHP object debug after install XDebug:

PHP XDebug 2

PHP XDebug 3

You can configure the way xdebug formats the var_dump() output by various php.ini settings; first of all, you can change the length of strings that XDebug displays, being 512 the default value; longer strings are automatically truncated. Whether you want XDebug to display the full string depends on the situation, and the size of data you work with. If you work with big strings, the var_dump() output can get long and hard to read, thus is is a good idea truncate strings for better overview. On the contrary, if you are looking for a specific value in the output, you’ll probably want the strings to be displayed in full length. To change the string length XDebug displays, add these lines to your XDebug configuration file, or use the ini_set('xdebug.var_display_max_data', 9999999999); function in your PHP scripts to override the global configuration:

# Linux XDebug configuration path: /etc/php5/conf.d/xdebug.ini
xdebug.var_display_max_data=**Integer-Number**
xdebug.var_display_max_children=**Integer-Number**

You can also:

  • Dump the values of the superglobal variables using the xdebug function xdebug_dump_superglobals().
  • Improves the way PHP displays error messages by automatically displaying a stack trace along with every PHP error message, warning and notice.

XDebug parameters

  • xdebug.var_display_max_data: debug string size, default 512
  • xdebug.var_display_max_children: number of array elements or object properties, default 128
  • xdebug.var_display_max_depth: nested levels of array elements and object relations, default 3
  • xdebug.show_local_vars: display the current values of local variables as well, default Off
  • xdebug.collect_params: display information about the parameters that were passed to a function, default 0
  • xdebug.dump_undefined: display undefined values, default Off
  • xdebug.dump.SERVER: display the global variables specified in the dump, values like: HTTP_HOST, SERVER_NAME
  • xdebug.dump_once: repeat dumping the global variables on every error, default On
  • xdebug.show_exception_trace: display a stack trace whenever an execption is thrown, default On
  • xdebug.max_nesting_level: limits recursion depth, default 100
Do you have a project idea? Let's make it together!