← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 2 of 10

PHP Debugging with Xdebug

Xdebug is a tool that lets you pause your code and step through it line by line, like pressing pause on a movie. You can peek at every variable to find out where things went wrong.

What is Xdebug?

Xdebug is a PHP extension that adds debugging, profiling, and code-analysis capabilities the language doesn't have on its own, and is close to a standard tool for serious PHP development.

Example: What is Xdebug?

php
<?php
// Print `extension_loaded('xdebug') ? "Xdebug is loaded" : "Xdebug not installed"` to the output
echo extension_loaded('xdebug') ? "Xdebug is loaded" : "Xdebug not installed";
// Print "\nAdds debugging, profiling, and code analysis to PHP" to the output
echo "\nAdds debugging, profiling, and code analysis to PHP";
?>

Stack Traces and Var Dumps

With Xdebug enabled, PHP's normal error output gets replaced with much richer messages -- including a full stack trace and nicely formatted variable dumps -- that make tracking down the source of a bug considerably faster.

Example: Stack Traces and Var Dumps

php
<?php
// Define the function `level3` with no parameters
function level3() { throw new Exception("Something broke"); }
// Define the function `level2` with no parameters
function level2() { level3(); }
// Define the function `level1` with no parameters
function level1() { level2(); }
// Try running this block; jump to `catch` if it throws
try {
    // Call `level1()`
    level1();
// Catch Exception $e
} catch (Exception $e) {
    // Print `$e->getTraceAsString()` to the output
    echo $e->getTraceAsString();
}
?>

Step Debugging

Step debugging lets you set a breakpoint in your IDE, run the script, and have execution pause exactly there so you can inspect variables and step through subsequent lines one at a time instead of guessing from logs.

Example: Step Debugging

php
<?php
$x = 5;
$y = 10;
$sum = $x + $y; // set a breakpoint here in your IDE to inspect $x and $y
echo $sum;
?>

Code Coverage

Code coverage tracking records which lines actually executed during a test run, revealing gaps in your test suite -- code paths nobody is testing at all.

Example: Code Coverage

php
<?php
function isEven($n) {
    if ($n % 2 === 0) {
        return true;
    }
    return false; // a test suite might never exercise this line
}
echo isEven(4) ? "even" : "odd";
?>

Performance Profiling

Xdebug's profiler records how much time and memory each function call consumes, producing a file you can load into a tool like KCachegrind to visually spot the actual bottleneck instead of guessing.

Example: Performance Profiling

php
<?php
// Define the function `slowFunction` with no parameters
function slowFunction() {
    for ($i = 0; $i < 100000; $i++) {}
}
// Declare `$start`, set to `microtime(true)`
$start = microtime(true);
// Call `slowFunction()`
slowFunction();
// Print "Took " . round(microtime(true) - $start, 4) . "s -- a profiler shows exactly where time goes" to the output
echo "Took " . round(microtime(true) - $start, 4) . "s -- a profiler shows exactly where time goes";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Leaving Xdebug enabled on production, which slows the server a lot.
  2. Forgetting to set the IDE port to match xdebug.client_port, so breakpoints never trigger.
  3. Leaving var_dump calls around instead of using stack traces.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.