Skip to Content
Course content

104: var_dump, print_r, and Debugging Output

Click on the "Edit" button in the top corner of the screen to edit your slide content.

We've all been there. You're deep in a logic flow, something isn't working, and your first instinct is to just echo the variable to see what's inside. It's the most intuitive move in the world. But as soon as you start dealing with anything more complex than a simple string, echo becomes your enemy.

The "Array" Trap

Imagine you're fetching a user's profile settings from a database. You've got an associative array that should contain their theme preference, notification settings, and account level. You write echo $userSettings; and hit refresh. Your browser proudly displays the word: Array.

// The naive approach
$userSettings = [
    'theme' => 'dark',
    'notifications' => true,
    'level' => 5
];

echo $userSettings; // Output: Array

This is the classic PHP rite of passage. echo is designed for strings. When you hand it an array, PHP essentially shrugs and tells you, "Yeah, this is an array," without telling you what's in it. It's frustrating, and it's a waste of your time. To actually see the data, we need tools that can "dump" the internal structure of the variable.

Readability vs. Precision

This is where print_r() and var_dump() come in. I usually treat these as two different tools for two different moods. When I just want a quick, human-readable glance at the keys and values of an array, I use print_r(). It's cleaner and gets straight to the point.

print_r($userSettings);
/* 
Output:
Array
(
    [theme] => dark
    [notifications] => 1
    [level] => 5
)
*/

But here is the catch: print_r() lies to you by omission. Notice how the boolean true for notifications became a 1? If that value had been false or null, print_r() would often show absolutely nothing for that key. In a production bug, the difference between false, null, and an empty string is usually where the actual bug is hiding.

When I'm hunting a real bug, I switch to var_dump(). It's uglier, but it's honest. It tells you the data type and the length of the strings. It doesn't guess.

var_dump($userSettings);
/*
Output:
array(3) {
  ["theme"]=> string(4) "dark"
  ["notifications"]=> bool(true)
  ["level"]=> int(5)
}
*/

I'll be honest: var_dump output is a nightmare to read in a browser because HTML collapses all the whitespace. It just looks like one giant, rambling sentence. The pro move here is to wrap your output in HTML <pre> tags. This tells the browser to respect the line breaks and indentation, turning a wall of text into a structured map.

echo '<pre>';
var_dump($userSettings);
echo '</pre>';

I've spent hours of my career chasing "ghost" bugs that were simply null values masquerading as empty strings. Use print_r for a sanity check, but use var_dump when you actually need to solve a problem.




📋 Practical Task

Exercise: Hunting the Null Value in a User Configuration Array

You have been given a configuration array that is causing a crash in a payment processing function. The function expects a numeric value for discount_rate, but something is wrong. Use var_dump() wrapped in <pre> tags to inspect the array and determine exactly why the discount_rate is failing.

<?php
$config = [
    'api_key' => 'sk_test_4eC39HqLyjWDarjtT1zdp7dc',
    'timeout' => 30,
    'discount_rate' => null, 
    'enabled' => false,
    'retry_attempts' => '3'
];

// Your code here: 
// 1. Wrap the output in <pre> tags.
// 2. Use the most precise dumping method to see the types.
?>
Rating
0 0

There are no comments for now.

to be the first to leave a comment.