php var_dump() 与 print_r()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3406171/
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
php var_dump() vs print_r()
提问by ina
What is the difference between var_dump()
and print_r()
in terms of spitting out an array as string?
将数组作为字符串吐出var_dump()
和print_r()
在这方面有什么区别?
回答by Sarfraz
The var_dump
function displays structured information about variables/expressions including its typeand value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
该var_dump
函数显示有关变量/表达式的结构化信息,包括其类型和值。使用缩进的值递归地探索数组以显示结构。它还显示哪些数组值和对象属性是引用。
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 keysand elements. Similar notation is used for objects.
该print_r()
约的方式,是由人类可读的可变显示的信息。数组值将以显示键和元素的格式呈现。类似的符号用于对象。
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
更多信息
回答by gilzero
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. Yet var_dump()
can show data types.
这var_dump()
不能做。还var_dump()
可以显示数据类型。
回答by David Yell
回答by Danny Nimmo
If you're asking when you should use what, I generally use print_r()
for displaying values and var_dump()
for when having issues with variable types.
如果您问什么时候应该使用什么,我通常print_r()
用于显示值以及var_dump()
在遇到变量类型问题时。
回答by Josh K
var_dump
displays structured information about the object / variable. This includes typeand values. Like print_r
arrays are recursed through and indented.
var_dump
显示有关对象/变量的结构化信息。这包括类型和值。像print_r
数组一样递归和缩进。
print_r
displays human readableinformation 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
将输出类型和值,而print_r
不会。
回答by Akshay Khale
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
会转储多个参数,其中 asprint_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. The returned value of print_r
will be in stringformat.
var_dump
不能返回任何值,它只能转储/打印值,如果我们将第二个参数设置print_r
为true,则 print_r 可以返回变量信息。的返回值print_r
将采用字符串格式。
The information printed by print_r
is much more in readable format where as var_dump
prints raw values.
打印的信息print_r
更多是可读格式,而 asvar_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
function 可用于许多上下文,其中 asvar_dump
可用于调试目的,主要是因为它不能返回值。
回答by Christian Nagel
I'd aditionally recommend putting the output of var_dump() or printr into a pretag when outputting to a browser.
我还建议在输出到浏览器时将 var_dump() 或打印机的输出放入pre标记中。
print "<pre>";
print_r($dataset);
print "</pre>";
Will give a more readable result.
将给出更具可读性的结果。
回答by ???????? ou??
var_dump($var)
shows in-depth details, by providing additional details of
var_dump($var)
显示深入的细节,通过提供额外的细节
- data type of the value (including the descendant elements)
- number of elements in a variable
- length of the value
- 值的数据类型(包括后代元素)
- 变量中的元素数
- 值的长度
回答by Neil Bauers
With large arrays, print_r can show far more information than is useful. You can truncate it like this, showing the first 2000 characters or however many you need.
对于大型数组,print_r 可以显示比有用的信息多得多的信息。您可以像这样截断它,显示前 2000 个字符或您需要的任何字符。
echo "<pre>" . substr(print_r($dataset, 1), 0, 2000) . "</pre>";
回答by HeadAndTail
var_dump() :-
var_dump() :-
- This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
- This function display number of element in a variable.
- This function display length of variable.
- Can't return the value only print the value.
- it is use for debugging purpose.
- 此函数显示有关一个或多个表达式的结构化信息,包括其类型和值。数组和对象通过缩进以显示结构的值进行递归探索。
- 此函数显示变量中元素的数量。
- 该函数显示变量的长度。
- 不能返回值只能打印值。
- 它用于调试目的。
Example :-
例子 :-
<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
?>
output :-
输出 :-
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
print_r() :-
打印_r() :-
- Prints human-readable information about a variable.
- Not display number of element in a variable as var_dump().
- Not display length of variable in a variable as var_dump().
- Return the value if we set second parameter to true in printf_r().
- 打印有关变量的人类可读信息。
- 不将变量中的元素数显示为 var_dump()。
- 不将变量中的变量长度显示为 var_dump()。
- 如果我们在 printf_r() 中将第二个参数设置为 true,则返回该值。
Example :-
例子 :-
<pre>
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>
</pre>
Output:-
输出:-
<pre>
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</pre>