php var_dump,var_export & print_r 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5039431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:36:27  来源:igfitidea点击:

Difference between var_dump,var_export & print_r

php

提问by Manish Trivedi

What is the difference between var_dump, var_exportand print_r?

之间有什么区别var_dumpvar_exportprint_r

回答by phihag

var_dumpis for debugging purposes. var_dumpalways prints the result.

var_dump用于调试目的。var_dump总是打印结果。

// 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_ris 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_rby default prints the result, but allows returning as string instead by using the optional $returnparameter.

print_r也用于调试目的,但不包括成员的类型。如果您知道数组中元素的类型,那么使用它是一个好主意,但否则可能会产生误导。print_r默认情况下打印结果,但允许使用可选$return参数作为字符串返回。

Array (
    [0] =>
    [1] =>
    [2] => 42
    [3] => Array ([0] => 42)
)

var_exportprints valid php code. Useful if you calculated some values and want the results as a constant in another script. Note that var_exportcan not handle reference cycles/recursive arrays, whereas var_dumpand print_rcheck for these. var_exportby default prints the result, but allows returning as string instead by using the optional $returnparameter.

var_export打印有效的 php 代码。如果您计算了一些值并希望结果作为另一个脚本中的常量,则很有用。请注意,var_export不能处理引用循环/递归数组,而var_dumpprint_r检查这些。var_export默认情况下打印结果,但允许使用可选$return参数作为字符串返回。

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

Personally, I think var_exportis the best compromise of concise and precise.

我个人认为var_export是简洁和精确的最佳折衷。

回答by Nanne

var_dumpand var_exportrelate like this (from the manual)

var_dumpvar_export像这样联系(来自手册

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.

var_export() 获取有关给定变量的结构化信息。它类似于 var_dump() ,但有一个例外:返回的表示是有效的 PHP 代码。

They differ from print_rthat var_dumpexports more information, like the datatype and the size of the elements.

他们从不同print_r的是var_dump出口更多的信息,如数据类型和元素的大小。