php 如何正确使用 print_r 或 var_dump?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14743342/
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 07:52:18  来源:igfitidea点击:

How do I properly use print_r or var_dump?

phpprintingvar-dump

提问by Michael Corvis

I use the following snippet quite often when I am debugging:

我在调试时经常使用以下代码段:

echo "<pre>" . var_dump($var) . "</pre>";

And I find I usually get a nice readable output. But sometimes I just don't. I'm particularly vexed at the moment by this example:

我发现我通常会得到一个很好的可读输出。但有时我只是不这样做。我现在对这个例子特别恼火:

<?php

$username='xxxxxx';
$password='xxxxxx';

$data_url='http://docs.tms.tribune.com/tech/tmsdatadirect/schedulesdirect/tvDataDelivery.wsdl';
$start=gmdate("Y-m-d\TH:i:s\Z",time());
$stop =gmdate("Y-m-d\TH:i:s\Z",time()+3600*24);

$client = new SoapClient($data_url, array('exceptions' => 0,
                                          'user_agent' => "php/".$_SERVER[SCRIPT_NAME],
                                          'login'      => strtolower($username),
                                          'password'   => $password));
$data = $client->download($start,$stop);

print_r($data);

?>

I don't want to reveal my credentials of course, but I am told print_r in this case will do the same as my usual snippet when in fact neither print_r nor my snippet produce anything other than runon data with no formatting at all. How can I make it pretty?!

我当然不想透露我的凭据,但有人告诉我,在这种情况下,print_r 将与我通常的代码段执行相同的操作,而实际上,print_r 和我的代码段都不会产生除 runon 数据之外的任何内容,而根本没有格式。怎么做才能好看?!

回答by rohitarora

var_dump always show you array in formatted data but too much extra stuff

var_dump 总是以格式化数据显示数组,但有太多额外的东西

var_dump($data);

but if you want formatted data here you need to use <pre>tags

但是如果你想在这里格式化数据,你需要使用<pre>标签

echo '<pre>';
print_r($data);
echo '</pre>';

回答by Nathan

var_dump() echos output directly, so if you want to capture it to a variable to provide your own formatting must use output buffers:

var_dump() 直接回显输出,因此如果要将其捕获到变量以提供自己的格式,则必须使用输出缓冲区:

   ob_start();
   var_dump($var);
   $s = ob_get_clean();

Once this is done the variable $s now contains the output of var_dump(), so can safely:

完成此操作后,变量 $s 现在包含 var_dump() 的输出,因此可以安全地:

   echo "<pre>" . $s . "</pre>";

回答by Bhuppi

var_dump is used when you want the more detail about any variable .

当您需要有关任何变量的更多详细信息时,可以使用 var_dump。

<?php 
    $temp = "hello" ;
    echo var_dump($temp);
    ?>

it output as follows string(5) "hello"means it print the data type of variable and length of string and what is the content in the variable.

它输出如下string(5) "hello"表示它打印变量的数据类型和字符串的长度以及变量中的内容。

while print_r($expression) is used for printing the data like array or any other object data type which can not directly printed by echo statement.

而 print_r($expression) 用于打印数据,如数组或任何其他无法通过 echo 语句直接打印的对象数据类型。

回答by Siki

Well, print_r() is used to print and array, but in order to display the array in a pretty way you also need html tags.

嗯,print_r() 用于打印和数组,但是为了以漂亮的方式显示数组,您还需要 html 标签。

Just do the following:

只需执行以下操作:

echo "<pre>";
print_r($data);
echo "</pre>";