Detailed output with var_dump()
Learn to display a variable's type and value in detail with var_dump().
The var_dump() function
var_dump() shows a variable's type and value in detail. It is the most common way to debug.
Example: var_dump("Book"); prints string(4) "Book".
For numbers the value appears directly: var_dump(42); prints int(42).
For bool you see bool(true) or bool(false), not the 1 or empty string that echo produces.
print_r or var_dump?
PHP has two common output functions: print_r and var_dump.
print_r gives simpler output; it does not show type info. It is readable for arrays but can mislead you with bool and null.
var_dump shows type, length and value completely. When debugging, always prefer var_dump.
Tip: echo only prints the value; it gives no type info. Use var_dump when you need type details.
Exercise Get the var_dump output of two variables
Assign "Guide" to $title and 240 to $pages. Write a var_dump() call for each variable.
Show example solution
Write one var_dump(...) call per line.
Example solution:
<?php
$title = "Guide";
$pages = 240;
var_dump($title);
var_dump($pages);
Test yourself
-
What is the output of var_dump("Book");?
var_dump("Book");- Book
- string Book
- string(4) "Book"
- var: Book
Show answer
string(4) "Book" — Correct! var_dump shows the type, length and value together.
-
Get detailed output of $user
____($user);Show answer
var_dump($user); -
Which is best to also see the type of a bool variable?
$ok = true;- echo $ok;
- print $ok;
- print_r($ok);
- var_dump($ok);
Show answer
var_dump($ok); — Correct! var_dump prints bool(true); the type is clear.