Menu

PHP var_dump() vs print_r(): Print Arrays and Debug

print_r($array) prints an array in a readable form, var_dump($value) also shows every type and length, and var_export prints valid PHP code. Learn when to use each, how to get print_r as a string, and how to pretty print in HTML with <pre>.

This page includes runnable editors - edit, run, and see output instantly.

print_r($array) prints an array or object with its keys and values in a readable form. var_dump($value) prints the same structure plus the type and length of every value. Use print_r to see what is in an array, and var_dump when you need to know whether a value is "1" or 1.

Compare the two outputs for admin: print_r printed nothing after the arrow, var_dump printed bool(false). That difference is the main reason to reach for var_dump while debugging.

print_r vs var_dump vs var_export

The three functions show the same data with different detail:

print_rvar_dumpvar_export
Shows typesnoyesas PHP literals
false, null, ""print nothingbool(false), NULL, string(0) ""false, NULL, ''
Can return a stringprint_r($x, true)novar_export($x, true)
Output is valid PHPnonoyes
Several values in one callnovar_dump($a, $b)no

print_r is easiest to read, var_dump is the most precise, and var_export gives you code you can paste back into a file.

Get print_r output as a string

Pass true as the second argument and print_r returns the text instead of printing it. That is how you write an array to a log file or put it inside a larger message.

var_export($value, true) returns a string in the same way. var_dump always prints, so to capture it you need output buffering: ob_start(); var_dump($x); $text = ob_get_clean();.

Pretty print an array in HTML with pre

In a browser, the line breaks and spaces of print_r collapse into one long line, because HTML ignores whitespace. Wrap the output in <pre> to keep the layout. Run this and look at the Page tab.

htmlspecialchars matters as soon as the data might contain < or &, for example user input: without it the browser would treat that text as HTML. For data you send to JavaScript or an API, the json_encode form is often the most useful.

Dump several values at once

var_dump accepts any number of arguments and prints each in turn, which is quick for comparing two values side by side.

The output shows why == says they are equal and === does not: one is string(2) "42", the other int(42).

All three functions print objects too. var_dump and print_r mark private and protected properties ([label:Point:private]), which helps when a property seems to be missing.

The #1 in the var_dump output is the object's id. Two variables that show the same id point to the same object.

Common mistake: echo on an array

echo turns its argument into a string. An array has no string form, so PHP prints the word Array and raises a warning:

<?php
$colors = ["red", "green", "blue"];
echo $colors;

PHP prints:

Warning: Array to string conversion in /home/index.php on line 3
Array

Print the contents with one of the functions above, or join the values yourself:

implode is for showing a list to a user, json_encode for logs and APIs, and print_r or var_dump for your own debugging.

Frequently Asked Questions

What is the difference between var_dump and print_r?

print_r shows keys and values in a short, readable form. var_dump also shows each value's type and length, such as string(3) "Ada" and bool(false), so it is the one to use when the type matters. print_r(false) prints nothing at all, while var_dump(false) prints bool(false).

How do I get the output of print_r as a string?

Pass true as the second argument: $text = print_r($array, true); returns the text instead of printing it, so you can write it to a log with error_log($text) or put it in a variable. var_export($value, true) works the same way; var_dump has no such option.

How do I print an array nicely in the browser?

Wrap the output in a <pre> tag so the browser keeps the line breaks and indentation: echo '<pre>' . print_r($array, true) . '</pre>';. Escape it with htmlspecialchars if the data can contain HTML.

Why does echo on an array print Array?

echo converts its argument to a string, and an array converts to the word Array with the warning "Array to string conversion". Use print_r, var_dump, implode(', ', $arr) or json_encode($arr) to print the contents.

What does var_export do in PHP?

It prints a value as valid PHP code, for example array ( 0 => 'a', ), that you can paste back into a script. With true as the second argument it returns the code as a string, which is how some tools write cached config files.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED