PHP 有漂亮的印刷品吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1168175/
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
Is there a pretty print for PHP?
提问by Aaron Lee
I'm fixing some PHP scripts and I'm missing ruby's pretty printer. i.e.
我正在修复一些 PHP 脚本,但缺少 ruby 漂亮的打印机。IE
require 'pp'
arr = {:one => 1}
pp arr
will output {:one => 1}. This even works with fairly complex objects and makes digging into an unknown script much easier. Is there some way to duplicate this functionality in PHP?
将输出 {:one => 1}。这甚至适用于相当复杂的对象,并使挖掘未知脚本变得更加容易。有没有办法在 PHP 中复制这个功能?
采纳答案by Andrew Moore
Both print_r()and var_dump()will output visual representations of objects within PHP.
双方print_r()并var_dump()会输出内PHP对象的可视化表示。
$arr = array('one' => 1);
print_r($arr);
var_dump($arr);
回答by Joel Hernandez
This is what I use to print my arrays:
这是我用来打印我的数组:
<pre>
<?php
print_r($your_array);
?>
</pre>
The magic comes with the pretag.
魔术来自pre标签。
回答by Sean McSomething
For simplicity, print_r()and var_dump()can't be beat. If you want something a little fancier or are dealing with large lists and/or deeply nested data, Krumowill make your life much easier - it provides you with a nicely formatted collapsing/expanding display.
为简单起见,print_r()和var_dump()是无可匹敌的。如果您想要一些更高级的东西,或者正在处理大型列表和/或深度嵌套的数据,Krumo将使您的生活变得更加轻松 - 它为您提供格式良好的折叠/展开显示。
回答by Guillaume Chevalier
The best I found yet is this:
我发现的最好的是这个:
echo "<pre>";
print_r($arr);
echo "</pre>";
And if you want it more detailed:
如果你想更详细:
echo "<pre>";
var_dump($arr);
echo "</pre>";
Adding a <pre>HTML tag in a web development environment will respect the newlines \nof the print function correctly, without having to add some html <br>
<pre>在 web 开发环境中添加HTML 标签会\n正确尊重打印功能的换行符,而无需添加一些 html<br>
回答by Stephen Katulka
For PHP, you can easily take advantage of HTML and some simple recursive code to make a pretty representation of nested arrays and objects.
对于 PHP,您可以轻松利用 HTML 和一些简单的递归代码来漂亮地表示嵌套数组和对象。
function pp($arr){
$retStr = '<ul>';
if (is_array($arr)){
foreach ($arr as $key=>$val){
if (is_array($val)){
$retStr .= '<li>' . $key . ' => ' . pp($val) . '</li>';
}else{
$retStr .= '<li>' . $key . ' => ' . $val . '</li>';
}
}
}
$retStr .= '</ul>';
return $retStr;
}
This will print the array as a list of nested HTML lists. HTML and your browser will take care of indenting and making it legible.
这会将数组打印为嵌套 HTML 列表的列表。HTML 和您的浏览器将负责缩进并使其清晰易读。
回答by Turnor
回答by pfrenssen
Remember to set html_errors = onin php.ini to get pretty printing of var_dump() in combination with xdebug.
请记住html_errors = on在 php.ini 中进行设置,以便结合 xdebug 获得漂亮的 var_dump() 打印。
回答by Mouneer
Best way to do this is
最好的方法是
echo "<pre>".print_r($array,true)."</pre>";
Example:
例子:
$array=array("foo"=>"999","bar"=>"888","poo"=>array("x"=>"111","y"=>"222","z"=>"333"));
echo "<pre>".print_r($array,true)."</pre>";
Result:
结果:
Array
(
[foo] => 999
[bar] => 888
[poo] => Array
(
[x] => 111
[y] => 222
[z] => 333
)
)
数组
(
[foo] => 999
[bar] => 888
[poo] => 数组
(
[x] => 111
[y] => 222
[z] => 333
)
)
Read more about print_r.
阅读更多关于print_r 的信息。
About the second parameter of print_r "true" from the documentation:
关于文档中 print_r "true" 的第二个参数:
When this parameter is set to TRUE, print_r() will return the information rather than print it.
当此参数设置为 TRUE 时,print_r() 将返回信息而不是打印它。
回答by Laurence
This is a little function I use all the time its handy if you are debugging arrays. The title parameter gives you some debug info as what array you are printing. it also checks if you have supplied it with a valid array and lets you know if you didn't.
如果您正在调试数组,这是我一直使用的一个小函数,它很方便。title 参数为您提供了一些调试信息,如您正在打印的数组。它还会检查您是否提供了一个有效的数组,如果没有,它会通知您。
function print_array($title,$array){
if(is_array($array)){
echo $title."<br/>".
"||---------------------------------||<br/>".
"<pre>";
print_r($array);
echo "</pre>".
"END ".$title."<br/>".
"||---------------------------------||<br/>";
}else{
echo $title." is not an array.";
}
}
Basic usage:
基本用法:
//your array
$array = array('cat','dog','bird','mouse','fish','gerbil');
//usage
print_array("PETS", $array);
Results:
结果:
PETS
||---------------------------------||
Array
(
[0] => cat
[1] => dog
[2] => bird
[3] => mouse
[4] => fish
[5] => gerbil
)
END PETS
||---------------------------------||
回答by Question Mark
error_log(print_r($variable,true));
to send to syslog or eventlog for windows
发送到 Windows 系统日志或事件日志

