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

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

php var_dump() vs print_r()

phparrays

提问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_dumpfunction 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

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"Exampleis here.

所以你会得到 =>(string)"var"例子在这里。

print_r()will just output the content.

print_r()只会输出内容。

Would output => "var"Exampleis here.

将输出 =>"var"示例在这里。

回答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_dumpdisplays structured information about the object / variable. This includes typeand values. Like print_rarrays are recursed through and indented.

var_dump显示有关对象/变量的结构化信息。这包括类型和值。像print_r数组一样递归和缩进。

print_rdisplays 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_dumpwill output type as well as values while print_rdoes not.

要注意的最重要的事情是var_dump将输出类型和值,而print_r不会。

回答by Akshay Khale

Significant differences between var_dumpand print_r

var_dump和之间的显着差异print_r

both the functions dumps information about the variable, but var_dumpmultiple parameters which will be dumped, where as print_rcan 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_dumpcan'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_rto true. The returned value of print_rwill be in stringformat.

var_dump不能返回任何值,它只能转储/打印值,如果我们将第二个参数设置print_rtrue,则 print_r 可以返回变量信息。的返回值print_r将采用字符串格式。

The information printed by print_ris much more in readable format where as var_dumpprints raw values.

打印的信息print_r更多是可读格式,而 asvar_dump打印原始值。

print_rfunction can be used in many contexts where as var_dumpcan be used in debugging purposes mainly since it can't return value.

print_rfunction 可用于许多上下文,其中 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() :-

  1. 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.
  2. This function display number of element in a variable.
  3. This function display length of variable.
  4. Can't return the value only print the value.
  5. it is use for debugging purpose.
  1. 此函数显示有关一个或多个表达式的结构化信息,包括其类型和值。数组和对象通过缩进以显示结构的值进行递归探索。
  2. 此函数显示变量中元素的数量。
  3. 该函数显示变量的长度。
  4. 不能返回值只能打印值。
  5. 它用于调试目的。

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() :-

  1. Prints human-readable information about a variable.
  2. Not display number of element in a variable as var_dump().
  3. Not display length of variable in a variable as var_dump().
  4. Return the value if we set second parameter to true in printf_r().
  1. 打印有关变量的人类可读信息。
  2. 不将变量中的元素数显示为 var_dump()。
  3. 不将变量中的变量长度显示为 var_dump()。
  4. 如果我们在 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>