← Back to PHP Course | Chapter 13: Error Handling | Lesson 5 of 5

PHP Debugging Techniques

Debugging is being a detective who hunts down why code is misbehaving. You print out values, read messages, and look at clues until you find the culprit.
Syntax
php
var_dump($variable);
print_r($variable);
debug_backtrace();

if (DEBUG_MODE) {
    var_dump($variable);
}

Simple Print Debugging

var_dump() and print_r() are the fastest way to inspect a variable's actual contents -- var_dump() additionally reveals data types and array/object structure, which matters when a bug turns out to be a type mismatch rather than a wrong value.

Example: Simple Print Debugging

php
<?php
// Declare `$data` as an array: `["id" => 1, "active" => true]`
$data = ["id" => 1, "active" => true];
// Print a detailed dump (with types) of `$data`
var_dump($data);
// Print a human-readable dump of `$data`
print_r($data);
?>

The Backtrace Utilities

debug_backtrace() returns the full chain of function calls that led to the current line, which is invaluable when you land inside a deeply nested call and need to understand how execution got there.

Example: The Backtrace Utilities

php
<?php
// Define the function `a` with no parameters
function a() { b(); }
// Define the function `b` with no parameters
function b() { print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); }
// Call `a()`
a();
?>

Conditional Debugging

Wrapping debug output in a check against a constant like DEBUG_MODE lets you leave diagnostic code in place without it ever running (or leaking information) in production.

Example: Conditional Debugging

php
<?php
// Call `define('DEBUG_MODE', true)`
define('DEBUG_MODE', true);
// Check whether `DEBUG_MODE`
if (DEBUG_MODE) {
    // Print "Debug: variable value is 42" to the output
    echo "Debug: variable value is 42";
}
?>

Tracking Memory and Time

memory_get_usage() and microtime(true) let you measure exactly how much memory a script consumes and how long a section takes to run, turning vague 'this feels slow' complaints into concrete numbers you can optimize against.

Example: Tracking Memory and Time

php
<?php
// Declare `$start`, set to `microtime(true)`
$start = microtime(true);
// Declare `$mem`, set to `memory_get_usage()`
$mem = memory_get_usage();
for ($i = 0; $i < 10000; $i++) {}
// Print "Time: " . round(microtime(true) - $start, 4) . "s\n" to the output
echo "Time: " . round(microtime(true) - $start, 4) . "s\n";
// Print "Memory: " . (memory_get_usage() - $mem) . " bytes" to the output
echo "Memory: " . (memory_get_usage() - $mem) . " bytes";
?>

Safe Variable Inspection

var_export() produces a string of valid PHP code representing a variable's value, which is handy for generating cache files or config arrays, not just for eyeballing a value during debugging.

Example: Safe Variable Inspection

php
<?php
// Declare `$config` as an array: `["theme" => "dark", "retries" => 3]`
$config = ["theme" => "dark", "retries" => 3];
// Print `var_export($config, true)` to the output
echo var_export($config, true);
?>
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 var_dump() or print_r() calls in code that goes to production, which shows internal data to visitors.
  2. Debugging output before header() calls, which triggers a headers already sent warning.
  3. Forgetting <pre> around print_r output in a browser, so the layout is unreadable.
Chapter Summary
  • Error handling begins with understanding PHP's errors, and try-catch handles exceptions.
  • Custom exceptions create specific error types.
  • Error logging and debugging techniques help you find problems.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

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.