Other Articles in the SeriesPart One: Introducing xdebugPart Two: Tracing PHP Applications with xdebug Part Three: Profiling PHP Applications With xdebug Part Four: Debugging PHP applications with xdebug Part Five: Creating Code Coverage Statistics with xdebug This article is the first installment of a five-part series of articles covering xdebug, a free and open source swiss army knife tool for PHP developers. xdebug is a PHP extension created by Derick Rethans, one of the PHP core developers. This week, we will show you how to install xdebug and introduce you to some of the basic features. In the subsequent parts of this article series, we will have a closer look at one of xdebug‘s main features, namely tracing, profiling, debugging, and code coverage. Installing the xdebug extensionFirst of all, we need to install xdebug. As I write this article, the current version is 2.0.1. Since the internal PHP APIs may change between different PHP releases, you must make sure that the version of xdebug you are installing matches the PHP version you are using. xdebug does not work with any PHP versions before 4.3, and will probably not yet work with PHP 6. This is not a real problem, however, since PHP 4 will reach its end of life in 2008, and PHP 6 will probably not be available before the end of 2008. This gives you enough time to get used to xdebug so you can use it as a helpful tool when it comes to migrating your PHP code to work with the next major or minor PHP release. Installing on UnixBefore we dig into xdebug‘s features, let us get the installation done. On Unix, you can try installing xdebug through PECL, the PHP extension community library. The PECL installation does not work on all systems, though. If the PECL installation does not work out for you, you must compile xdebug from source. But first, try the PECL installation:
If the PECL installation does not work for you, you need to compile xdebug from source. In addition to a C compiler, you will need appropriate versions of the usual build tools (Autoconf, Automake and Libtool). If they are not already installed on your system, you can usually install them by running Furthermore, two helper programs, Please note that
wget http:///link.php?url=xdebug201 tar -xzf xdebug-2.0.1.tgz cd xdebug-2.0.1 phpize ./configure --enable-xdebug --with-php-config=/usr/bin/php-config make cp modules/xdebug.so /usr/lib/apache2/modules/xdebug.so The Installing on WindowsIf you are a Windows user, you can download a compiled DLL from . Select the PHP version you are using and click the appropriate link in the Windows modules section in the right column of the page. You must use the non-debug version of PHP with xdebug. If you have downloaded PHP from , debugging should not be enabled. When in doubt, check the Debug Build entry in the I would recommend putting the downloaded DLL into PHP‘s extension directory ext, which should be a subdirectory of your PHP directory. You can put the DLL to any directory, provided that you state the full path to the DLL in php.ini. Activating the xdebug extensionNow you have the xdebug extension ready, either as a shared object on Unix or a DLL on Windows. To activate xdebug, you must add an entry to php.ini. On Windows, use:
On Unix, use:
instead. The path to PHP‘s extension directory or Apache‘s module directory may differ on your system. Make sure you specify the full absolute path, not a relative path. Please note that on Windows, we use You should not load any other Zend extensions while working with xdebug, because they would probably use the same internal mechanisms in the Zend engine, which usually calls for trouble. Not all Zend extensions work together with xdebug. Since you will probably use xdebug on a development system rather than a live system, this is no serious limitation. The most important rule is to not mix xdebug with other PHP debugger extensions. Having restarted your web server because we changed php.ini, you can check the output of
Take care when you are updating PHP with xdebug installed. If the internal APIs change between the PHP versions (which does not happen on every new version, but will certainly happen when you are close to a project deadline), the new version of PHP will probably not start or at least give you funny errors. If need be, you can always get away with disabling xdebug, at least temporarily, to get an updated PHP version to work and re-enable xdebug as soon as a new version is released that works with the newer API version. There are quite some php.ini switches to configure xdebug, but most of them have sensible deafult values, so we can start using xdebug without worrying about configuration settings right now. We will take a look at the most important configuration settings as we need to. Improved var_dump() outputLet‘s face it: the most widely used PHP debugger is xdebug offers interesting alternatives to the extensive use of When the xdebug extension is loaded, the output of PHP‘s
You can configure the way xdebug formats the Wether 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 To change the string length xdebug displays, add
to php.ini and restart your web server so that this setting takes effect. Alternatively, just like you can change various php.ini settings of the PHP core at script runtime by
on top of your script to make sure the You can also control the number of array elements or object properties that xdebug displays. This is done by modifying the If you work with nested objects and arrays, you may want to modify the You can also dump the values of the superglobal variables using the xdebug function Using
in your PHP script or setting
in php.ini will display the value of
By default, xdebug does not dump undefined values, but just skips the respective array keys. To force display of undefined values, set You may wonder why xdebug supports configuring displaying select items of superglobal variables, when the same effect could be achieved with a number of Better error messagesxdebug also improves the way PHP displays error messages by automatically displaying a stack trace along with every PHP error message, warning and notice. This stack trace lists the history of function calls that lead to an error. As PHP programs become more and more object-oriented, sometimes errors tend to show up deep inside libraries or helper objects. The stack trace allows you to quickly find out where the code piece that caused an error was originally called from. Since version 5.0, PHP also has a native function Like PHP itself, xdebug only displays error messages when
to your script to change the setting at runtime.
Looking at this stack trace, you can see that the function In the previous section, we have already learned how to configure xdebug to display values of superglobal variables. In addition, you can configure xdebug to display the current values of local variables as well. To display local variables along with every error message, you need to add
to php.ini. You can use
By the way, the php.ini settings There is even more. With Here is the xdebug configuration used to create the following screenshot: xdebug.show_local_vars=On xdebug.dump.SERVER=HTTP_HOST, SERVER_NAME xdebug.dump_globals=On xdebug.collect_params=4
This screenshot was taken using the following xdebug configuration: Please note that the more information you ask xdebug to collect during script execution, the more memory and computing time it will use, thus slowing the execution of your script. Make sure to only enable xdebug on development systems, and never on production systems. It is not a wise idea to display error messages as part of the HTML result on production systems, and it is even less of a good idea to enrich this information with stack traces and potentially variable values. Imagine your web site displaying your database username and password to everybody just because you use an uninitialized variable! Since superglobal variables should not change at runtime, xdebug by default displays them only on the first error message, not in every error message. If you want xdebug to repeat dumping the global variables on every error, use
Please note that the extended error display of xdebug does not work if you define a custom error handler using Object-oriented code uses exceptions when something goes wrong. Since an exception is not an error, xdebug will not display a stack trace when an exception is thrown, but only if that exception is uncaught. That is because an uncaught exception is a fatal error. To can configure xdebug to display a stack trace whenever an execption is thrown, use
Recursion LimitAnother very useful feature is the fact that xdebug limits recursion depth, thus preventing endless recursion, which usually makes PHP segfault on Unix. On Windows, PHP even locks up the whole system when it falls into endless recursion, which is usually far worse than having PHP segfault. xdebug prevents endless recursion by stopping the script once a pre-defined nesting depth is reached. This is, in effect, a limit of the stack size. Every recursive function call will increase the stack size, but also nested regular function calls increase the stack size. The default value is 100 and this feature is enabled by default. You can modify this limit, which might be necessary if you work with a script that works on deeply nested recursive data structures.
Bear in mind, however, that xdebug does not prevent endless loops with Conclusionxdebug is a small, but very useful tool in PHP development. It should be installed and activated on every PHP installation that is used for development. You should not run xdebug on a production system, though, because most features degrade performance. Personally, I would never want to miss xdebug a single day on my development system, and if it is just for the better formatting of the Today, we have just scratched the surface of what xdebug can do to make your every day‘s life as a developer much easier. Next week, we will explore the tracing features of xdebug. Tracing helps you better understand your applications, and can be a replacement for putting loads of Make sure you check back here next week for the second part of this series of xdebug articles. Until then: Happy coding - with xdebug enabled. |
|