← Back to PHP Course | Chapter 2: Output & Input | Lesson 2 of 8

PHP var_dump & print_r

var_dump and print_r are like x-ray glasses for your variables: they show what is really inside, including its kind and every part of it. Programmers use them to see what went wrong when the answer looks odd.
Syntax
php
var_dump($variable);        // type and value
print_r($variable);         // readable structure
$text = print_r($variable, true);   // return instead of printing

What is var_dump()?

var_dump() is built for debugging, not for user-facing output — it prints a variable's exact type alongside its value, including string length and array/object structure.

That type information is exactly what you need to catch bugs like an unexpected string "0" masquerading as a falsy value.

Example: What is var_dump()?

php
<?php
// Declare `$value`, set to "0"
$value = "0";
// Print a detailed dump (with types) of `$value`
var_dump($value);
?>

What is print_r()?

print_r() favors readability over completeness: it shows an array or object's structure in an indented, human-friendly layout without cluttering the output with type annotations.

It's usually the first tool reached for when you just want to eyeball what's inside a variable.

Example: What is print_r()?

php
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>

print_r() with Return Parameter

Passing true as print_r()'s second argument suppresses the direct screen output and returns the formatted text as a string instead.

That's essential when you want to write the dump to a log file or embed it inside another string rather than printing it immediately.

Example: print_r() with Return Parameter

php
<?php
// Declare `$data` as an array: `["a" => 1, "b" => 2]`
$data = ["a" => 1, "b" => 2];
// Declare `$output`, set to `print_r($data, true)`
$output = print_r($data, true);
// Call `file_put_contents('log.txt', $output)`
file_put_contents('log.txt', $output);
// Print `$output` to the output
echo $output;
?>

Debugging Nested Arrays

For deeply nested arrays — an array of arrays of objects, say — both functions recursively expand every level, which is exactly what makes them more useful than a plain echo for inspecting complex data: you see the full shape of the structure, not just a top-level summary.

Example: Debugging Nested Arrays

php
<?php
$data = [
    "user" => ["name" => "Alice", "roles" => ["admin", "editor"]]
];
// Print a detailed dump (with types) of `$data`
var_dump($data);
?>

var_dump() on Objects

Run var_dump() on an object and you get its class name plus every property's visibility (public/protected/private) and current value.

That visibility detail is unique to var_dump() — print_r() shows the values but not which access level each property has.

Example: var_dump() on Objects

php
<?php
// Define the class `User`
class User {
    // Declare the public property `$name`, set to "Alice"
    public $name = "Alice";
    // Declare the private property `$password`, set to "secret"
    private $password = "secret";
}
// Print a detailed dump (with types) of `new User()`
var_dump(new User());
?>
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 production pages, exposing internal data structures to visitors.
  2. Calling print_r($arr) on a page and reading squashed output, when wrapping it in <pre> tags preserves the line breaks.
  3. Trying to echo an array directly, which prints only the word Array (with a warning) instead of its contents; use print_r or var_dump.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.