php var_dump vs print_r vs var_export

PHP functions for debug output:

differences between PHP var_dump vs print_r vs var_export:

var_dump is for debugging purposes. 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.

// 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 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. 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.

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.

array (
  0 => '',
  2 => false,
  2 => 42,
  3 => array (0 => '42',),
)

Leave a Comment