本文翻译自:php var_dump() vs print_r()
var_dump()
和print_r()
在将数组吐出为字符串方面有什么区别?
#1楼
参考:https://stackoom.com/question/ei6f/php-var-dump-vs-print-r
#2楼
Generally, print_r( )
output is nicer, more concise and easier to read, aka more human-readable but cannot show data types. 通常, print_r( )
输出更好,更简洁,更容易阅读,也更人性化但不能显示数据类型。
With print_r()
you can also store the output into a variable: 使用print_r()
您还可以将输出存储到变量中:
$output = print_r($array, true);
which var_dump()
cannot do. 哪个var_dump()
不能做。 Yet var_dump()
can show data types. 然而, var_dump()
可以显示数据类型。
#3楼
Significant differences between var_dump
and print_r
var_dump
和print_r
之间存在显着差异
both the functions dumps information about the variable , but var_dump
multiple parameters which will be dumped, where as print_r
can take two parameters out of which first parameter is the variable you want to dump and second is a boolean value. 这两个函数都转储有关变量的信息 ,但var_dump
将转储多个参数,其中print_r
可以取两个参数,其中第一个参数是要转储的变量,第二个参数是布尔值。
var_dump
can't return any value it can only dump/print the values where as print_r can return the variable information if we set second parameter of print_r
to true . var_dump
不能返回任何值,它只能转储/打印值,如果我们将print_r
第二个参数设置为true ,则print_r可以返回变量信息。 The returned value of print_r
will be in string format. print_r
的返回值将采用字符串格式。
The information printed by print_r
is much more in readable format where as var_dump
prints raw values. print_r
打印的信息更具可读性, var_dump
打印原始值。
print_r
function can be used in many contexts where as var_dump
can be used in debugging purposes mainly since it can't return value. print_r
函数可以在许多上下文中使用,因为var_dump
可以用于调试目的,主要是因为它不能返回值。
#4楼
var_dump()
will show you the type of the thing as well as what's in it. var_dump()
将显示事物的类型以及内容。
So you'll get => (string)"var"
Example is here. 所以你得到=> (string)"var"
示例在这里。
print_r()
will just output the content. print_r()
将只输出内容。
Would output => "var"
Example is here. 输出=> "var"
示例在这里。
#5楼
var_dump
displays structured information about the object / variable. var_dump
显示有关对象/变量的结构化信息。 This includes type and values. 这包括类型和值。 Like print_r
arrays are recursed through and indented. 就像print_r
数组一样,递归并缩进。
print_r
displays human readable information about the values with a format presenting keys and elements for arrays and objects. print_r
使用呈现数组和对象的键和元素的格式显示有关值的人类可读信息。
The most important thing to notice is var_dump
will output type as well as values while print_r
does not. 最var_dump
是var_dump
将输出类型和值,而print_r
则不会。
#6楼
The var_dump
function displays structured information about variables/expressions including its type and value . var_dump
函数显示有关变量/表达式的结构化信息,包括其类型和值 。 Arrays are explored recursively with values indented to show structure. 递归地探索数组,其中值缩进以显示结构。 It also shows which array values and object properties are references. 它还显示哪些数组值和对象属性是引用。
The print_r()
displays information about a variable in a way that's readable by humans. print_r()
以人类可读的方式显示有关变量的信息。 array values will be presented in a format that shows keys and elements . 数组值将以显示键和元素的格式显示。 Similar notation is used for objects. 类似的符号用于对象。
Example: 例:
$obj = (object) array('qualitypoint', 'technologies', 'India');
var_dump($obj)
will display below output in the screen. var_dump($obj)
将在屏幕下方显示输出。
object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}
And, print_r($obj)
will display below output in the screen. 并且, print_r($obj)
将在屏幕下方显示输出。
stdClass Object (
[0] => qualitypoint
[1] => technologies
[2] => India
)
More Info 更多信息