var_dump

Prints the type and value of one or more expressions in a readable, recursive format.

Signature
var_dump(mixed $value, mixed ...$values): void
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedThe expression to dump.
$valuesoptionalmixedFurther expressions to dump. Variadic — pass as many as you like and each is dumped in turn.

Return value

voidNo value is returned; output is written directly to the output buffer.

Why it beats print_r and echo

var_dump() is the only one of the three that shows you types. echo silently coerces, and print_r() prints false, null and "" as nothing at all, and renders 0 and "0" identically as 0. When you are chasing a bug where a comparison unexpectedly fails, that distinction is usually the whole answer — reach for var_dump() first and only fall back to print_r() when the structure is deep and you just want shape.

Reading the output

Strings carry their byte length: string(6) "héllo" is six bytes but five characters, which is how you spot an encoding problem. Arrays show their element count. Objects show #1-style instance handles, so two dumps sharing a handle are the same object, not two equal ones. Private properties render as ["name":"ClassName":private] and protected as ["name":protected].

Practical notes

It takes any number of arguments, so var_dump($a, $b, $c) in one call is fine. Output goes straight to stdout, but it obeys output buffering: wrap it in ob_start() / ob_get_clean() to capture the text into a string for a log. In a browser, wrap the page area in <pre> or the indentation collapses. If Xdebug is installed it replaces this function entirely and truncates at three levels deep by default.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Find out why a strict comparison fails by dumping both operands and comparing their types side by side.
  • Inspect the exact shape of a decoded JSON payload before writing code that walks it.
  • Confirm whether two variables point at the same object instance by comparing the #n handles in the output.

Frequently asked questions

What is the difference between var_dump and print_r?

var_dump() shows the type and size of every value and can take multiple arguments, while print_r() prints a compact, type-free structure. Use var_dump() when the type matters and print_r($x, true) when you just want a readable string of a large array.

How do I get var_dump output into a variable?

Wrap the call in output buffering: ob_start(); var_dump($x); $text = ob_get_clean();. var_dump() itself returns void, so there is no return-mode flag like print_r() has.

Why does my var_dump output stop after three levels?

That is Xdebug, which overrides var_dump() and limits depth by default. Raise xdebug.var_display_max_depth in your php.ini, or use print_r() or var_export() which Xdebug does not touch.

More examples

Hiding sensitive fields with __debugInfo()

Without __debugInfo(), var_dump() prints every private property including the raw token. Defining the magic method replaces the dumped shape entirely, so the token shows as asterisks — worth adding to any class holding credentials that might end up in a log.

Example · php
<?php
class ApiClient {
    public function __construct(
        public string $endpoint,
        private string $token,
    ) {}

    public function __debugInfo(): array {
        return ['endpoint' => $this->endpoint,
                'token' => str_repeat('*', strlen($this->token))];
    }
}

var_dump(new ApiClient('https://api.example', 'sk_live_secret'));

Capturing the output into a string

var_dump() returns void, but it respects output buffering, so ob_start() plus ob_get_clean() gives you the text as a string you can log or assert against. This is the standard workaround when you need the dump somewhere other than the response body.

Example · php
<?php
function dumpToString(mixed $value): string {
    ob_start();
    var_dump($value);
    return ob_get_clean();
}

$text = dumpToString(['a' => 1, 'b' => [2, 3]]);
echo strlen($text), " bytes captured\n";
echo str_replace("\n", ' | ', trim($text)), "\n";

Object handles reveal identity, not just equality

$a and $b print the same #1 handle; $c prints #2. That handle number is how you prove at a glance whether two variables reference one object or two — the loose comparison says true, the strict one says false.

Example · php
<?php
class Box { public function __construct(public int $n) {} }

$a = new Box(1);
$b = $a;          // same instance
$c = new Box(1);  // equal but different instance

var_dump($a, $b, $c);
var_dump($a == $c, $a === $c);

Discussion

  • Be the first to comment on this lesson.