开发者

Difference between var_dump,var_export & print_r

开发者 https://www.devze.com 2023-02-12 01:35 出处:网络
What is the difference between var_dump, var_export and 开发者_StackOverflow中文版print_r ?var_dump is for debugging purposes. var_dump always prints the result.

What is the difference between var_dump, var_export and 开发者_StackOverflow中文版print_r ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消