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
Difference between var_dump,var_export & print_r
提问by Manish Trivedi
What is the difference between var_dump
, var_export
and print_r
?
之间有什么区别var_dump
,var_export
和print_r
?
回答by phihag
var_dumpis for debugging purposes. var_dump
always 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_r
by default prints the result, but allows returning as string instead by using the optional $return
parameter.
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_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.
var_export打印有效的 php 代码。如果您计算了一些值并希望结果作为另一个脚本中的常量,则很有用。请注意,var_export
不能处理引用循环/递归数组,而var_dump
并print_r
检查这些。var_export
默认情况下打印结果,但允许使用可选$return
参数作为字符串返回。
array (
0 => '',
1 => false,
2 => 42,
3 => array (0 => '42',),
)
Personally, I think var_export
is the best compromise of concise and precise.
我个人认为var_export
是简洁和精确的最佳折衷。
回答by Nanne
var_dump
and var_export
relate like this (from the manual)
var_dump
并var_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_r
that var_dump
exports more information, like the datatype and the size of the elements.
他们从不同print_r
的是var_dump
出口更多的信息,如数据类型和元素的大小。