var_dump()
The var_dump() function is a built-in function in PHP that displays structured information about a variable or an expression, including its type and value. It
Introduction
var_dump() is a built-in PHP debugging function that prints structured information about one or more variables — including each value's type, length, and contents — directly to the output. Unlike echo, which only prints a value, var_dump() shows you exactly what kind of value you are looking at, which makes it the fastest way to answer "why isn't this working?" while developing.
This page covers the syntax, how each PHP type is rendered, working with arrays and objects, how it differs from related functions like print_r() and var_export(), and the gotchas to watch for.
Syntax
var_dump(mixed $value, mixed ...$values): void$value— the variable or expression to inspect....$values— any number of additional values; each is dumped in order.- Return value: none (
void).var_dump()writes straight to the output and gives you nothing back to assign.
Basic example
Here $var1 is an integer, $var2 a string, and $array an array. The output reports the type and value of each one:
int(10)
string(13) "Hello, world!"
array(3) {
[0]=>
string(5) "apple"
[1]=>
string(6) "banana"
[2]=>
string(6) "cherry"
}Read the output carefully — there is more detail than echo would give you:
int(10)— the value is the integer10.string(13) "Hello, world!"— a string 13 bytes long. Seeing the length is useful for spotting hidden whitespace or encoding issues.- For the array, each
[key]=>line shows the key, then the type and value of the element below it.
How each type is rendered
var_dump() formats every scalar type differently, which is why it is more informative than printing a value. The table summarizes the notation:
| PHP type | Example output |
|---|---|
| integer | int(42) |
| float | float(3.14) |
| string | string(5) "hello" |
| boolean | bool(true) |
| null | NULL |
| array | array(2) { ... } |
| object | object(ClassName)#1 (2) { ... } |
The boolean and NULL cases are the ones developers rely on most. echo true prints 1 and echo false / echo null print nothing, so it is impossible to tell false, null, and "" apart with echo — but var_dump() shows them clearly:
<?php
var_dump(true);
var_dump(false);
var_dump(null);
var_dump("");
var_dump(0);
var_dump("0");
?>bool(true)
bool(false)
NULL
string(0) ""
int(0)
string(1) "0"Notice how var_dump() distinguishes the integer 0 from the string "0" — a distinction that silently causes bugs in loose comparisons. See PHP Data Types and gettype() for more on type checking.
Dumping objects and nested structures
var_dump() recurses into objects and nested arrays, showing visibility (public / protected / private) for each property:
<?php
class User {
public string $name = "Ada";
protected int $age = 36;
private array $roles = ["admin", "editor"];
}
var_dump(new User());
?>object(User)#1 (3) {
["name"]=>
string(3) "Ada"
["age":protected]=>
int(36)
["roles":"User":private]=>
array(2) {
[0]=>
string(5) "admin"
[1]=>
string(6) "editor"
}
}The #1 is the object's internal instance id, and (3) is the number of properties.
Dumping multiple values at once
Because var_dump() is variadic, you can inspect several variables in a single call instead of writing one line per variable:
<?php
$id = 7;
$name = "Grace";
$active = true;
var_dump($id, $name, $active);
?>int(7)
string(5) "Grace"
bool(true)var_dump() vs. print_r() vs. var_export()
These three functions overlap, so choosing the right one matters:
| Function | Shows types? | Output is valid PHP? | Best for |
|---|---|---|---|
var_dump() | Yes | No | Debugging — most detail |
print_r() | No | No | A readable, human-friendly view |
var_export() | No | Yes | Generating code / config dumps |
Use var_dump() when you need to see exact types and lengths. Reach for print_r() when you just want a quick, less noisy look at an array's shape, and var_export() when you want output you can paste back into source code.
Capturing the output as a string
var_dump() returns nothing, so you cannot do $x = var_dump($y). To capture its output (for logging, say), wrap it in output buffering:
<?php
$data = ["status" => 200, "ok" => true];
ob_start();
var_dump($data);
$dump = ob_get_clean();
// $dump now holds the var_dump output as a string
echo strlen($dump) . " characters captured\n";
?>Notes and gotchas
- Output destination.
var_dump()writes to standard output. Run in a browser, modern PHP wraps the dump in<pre>tags so the structure is readable; on the CLI it prints plain text. - Leftover dumps ship to production. A stray
var_dump()left in code will leak internal data to users. Remove debug dumps before deploying, or gate them behind a debug flag. - Strings show byte length, not character count.
string(13)counts bytes; a multibyte (UTF-8) string can report a larger number than the visible character count. - Use Xdebug for nicer output. With the Xdebug extension installed,
var_dump()is automatically overloaded to produce color-coded, depth-limited output that is far easier to scan.
Conclusion
var_dump() is the go-to function for inspecting what a value actually is during PHP development. It reveals type, length, structure, and visibility in one call — detail that plain echo or print hides. Reach for print_r() when you want a cleaner read of an array and var_export() when you need reusable PHP code, but for raw debugging insight var_dump() is hard to beat.