var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.
The function var_dump() displays structured information (type and value) about one or more expressions/variables. Arrays and objects are explored recursively with values indented to show structure. All public, private and protected properties of objects will be returned in the output.
@JMTyler var_export returns a parsable string—essentially PHP code—while var_dump provides a raw dump of the data. So, for example, if you call var_dump on an integer with the value of 1, it would print int(1) while var_export just prints out 1 .
The var-dump is display the datatype as well as value while echo only display the value. Explanation: In Php the var-dump function is used for displaying the datatype as well the value . The var-dump () does not return anythings it means there is no datatype of var-dump() function.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With