tags:

views:

133

answers:

4

What is the difference between var_dump() and print_r() in terms of spitting out an array as string?

+2  A: 

var_dump() will show you the type of the thing as well as what's in it.

So you'll get => [string]"var" or similar http://php.net/manual/en/function.var-dump.php

print_r() will just output the content.

Would output => "var" http://www.php.net/manual/en/function.print-r.php

DavidYell
I think the better example would be `var_dump(0.0);` which outputs `float(0)` vs `print_r(0.0);` which outputs `0` (hence leading to possible type confusion)...
ircmaxell
+1  A: 

var_dump displays structured information about the object / variable. This includes type and values. Like print_r arrays are recursed through and indented.

print_r displays human readable information about the values with a format presenting keys and elements for arrays and objects.

The most important thing to notice is var_dump will output type as well as values while print_r does not.

Josh K
+7  A: 

The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

Example:

$obj = (object) array('qualitypoint', 'technologies', 'India');

var_dump($obj) will display below output in the screen.

object(stdClass)#1 (3) {
 [0]=> string(12) "qualitypoint"
 [1]=> string(12) "technologies"
 [2]=> string(5) "India"
}

And, print_r($obj) will display below output in the screen.

stdClass Object ( 
 [0] => qualitypoint
 [1] => technologies
 [2] => India
)

More Info

Sarfraz
+1  A: 

If you're asking when you should use what, I generally use print_r() for displaying values and var_dump() for when having issues with variable types.

dannybolabo