What is the difference between var_dump
, var_export
and print_r
?

- 357
- 1
- 3
- 12

- 3,481
- 5
- 23
- 29
-
14Sometimes its easier to ask somebody else to understand better then reading manuals. – Deepak Lamichhane Dec 06 '12 at 14:26
-
12@Your Common Sense S.O. answers are often clearer, more concise, actionable and basically always easier to reference than the PHP manual. – Mark Fox Apr 29 '13 at 22:45
2 Answers
var_dump is for debugging purposes. var_dump
always prints the result.
// var_dump(array('', false, 42, array('42')));
array(4) {
[0]=> string(0) ""
[1]=> bool(false)
[2]=> int(42)
[3]=> array(1) {[0]=>string(2) "42")}
}
print_r is for debugging purposes, too, but does not include the member's type. It's a good idea to use if you know the types of elements in your array, but can be misleading otherwise. print_r
by default prints the result, but allows returning as string instead by using the optional $return
parameter.
Array (
[0] =>
[1] =>
[2] => 42
[3] => Array ([0] => 42)
)
var_export prints valid php code. Useful if you calculated some values and want the results as a constant in another script. Note that var_export
can not handle reference cycles/recursive arrays, whereas var_dump
and print_r
check for these. var_export
by default prints the result, but allows returning as string instead by using the optional $return
parameter.
array (
0 => '',
1 => false,
2 => 42,
3 => array (0 => '42',),
)
Personally, I think var_export
is the best compromise of concise and precise.
-
54Note that `var_export`, due to its nature, will die a horrible recursive death on, well, recursive arrays. `print_r` and `var_dump` (though, not perfectly sure about latter, since I don't usually use it) don't have that issue. So don't `var_export($_GLOBALS);`, for example (which contains itself). :) – pinkgothic Feb 18 '11 at 16:28
-
@ftrotter I always thought the comment would explain that curiosity well, but there you go, added a short note to the answer. – phihag Apr 02 '13 at 01:47
-
1It should be added that you can make print_r() and var_export() return a string instead of outputting it, while var_dump() can't do it. Also, I don't like var_export() since it's confusing - if you try to export some undefined constant SOMECONST, you'll just get back a text string 'SOMECONST'. So it won't say NULL, 0, "", but it will actually presume it's a string (and I suppose throw a NOTICE too). – userfuser Jan 17 '14 at 11:53
-
1var_export is good for safely representing a string, like quote/backslash protection. – dkellner Oct 06 '15 at 18:43
-
Any idea why `var_export` has a trailing comma after the last array item? – WebChemist Dec 30 '15 at 01:14
-
@WebChemist Why not? It's easier to implement that way, and if I decide to use the output somewhere, I can simply add a line without worrying about having to modify the previously last line. – phihag Dec 30 '15 at 01:53
-
1Personally I prefer `var_dump` output. Too bad it does not allow returning the output as a string. As such I can relate to @iconoclast's sentiment in the comment above. But having to choose `var_export` over `var_dump` for this use case is acceptable to me. For completeness, note this related [feature request](https://bugs.php.net/bug.php?id=73811). – pjvleeuwen Dec 25 '16 at 14:07
-
`print_r` is useful to see the `structure` of your data in a clean and concise manner. It does not include types information, as said already, thus a lot more usable for casual investigation of data structures. – AnrDaemon Aug 04 '18 at 12:06
-
@Ricardo Martins Huh? Both with `var_export` and `var_dump`, this outputs `0.85`. In any case, the code `floatval(number_format(...))` is buggy – note what happens if the input is 1234.5! – phihag May 01 '19 at 10:13
var_dump
and var_export
relate like this (from the manual)
var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code.
They differ from print_r
that var_dump
exports more information, like the datatype and the size of the elements.