PHP:
Using a debugger

How to:

PHP comes with an interactive debugger called Xdebug. Here’s how to use it.

First, ensure you have Xdebug installed and configured in your php.ini file:

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-xxxxxxxx/xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes

Next, write a simple PHP script with a bug:

<?php
function add($a, $b) {
    return $a - $b; // Whoops! This should be a plus, not a minus
}

$result = add(1, 2);
echo "Result is: $result"; // Output should be 3, not -1

Using an IDE like PhpStorm, set a breakpoint by clicking next to the line number. Run the debugger and watch how variables change as you step through execution. When you step over the add function, you’ll notice that $result becomes -1, which is unexpected.

Deep Dive:

Historically, PHP was used primarily for small scripts, and debugging was a matter of adding var_dump() and print_r() statements throughout the code. Over time, with PHP becoming a key player in web development, more sophisticated tools like Xdebug and Zend Debugger came into use.

Alternatives to Xdebug include pcov and phpdbg. These offer various features but might not be as full-featured as Xdebug. phpdbg is a lightweight, PHP-specific debugger which is distributed with PHP since 5.6, and pcov is a code coverage driver.

When implementing a debugger, remember that you should never leave the debugger turned on in your production server, as it can expose security vulnerabilities and slow down performance.

See Also: