PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
104: var_dump, print_r, and Debugging Output
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.
?>There are no comments for now.