php 快捷方式 echo "<pre>";print_r($myarray);echo "</pre>";
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3785214/
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
Shortcut for echo "<pre>";print_r($myarray);echo "</pre>";
提问by mrN
Is there a Shortcut for
有快捷方式吗
echo "<pre>";
print_r($myarray);
echo "</pre>";
It is really annoying typing those just to get a readable format of an array.
只是为了获得数组的可读格式而输入这些真的很烦人。
回答by Casey Chu
This is the shortest:
这是最短的:
echo '<pre>',print_r($arr,1),'</pre>';
The closing tag can also be omitted.
结束标记也可以省略。
回答by Dennis Haarbrink
Nope, you'd just have to create your own function:
不,您只需要创建自己的函数:
function printr($data) {
echo "<pre>";
print_r($data);
echo "</pre>";
}
Apparantly, in 2018, people are still coming back to this question. The above would not be my current answer. I'd say: teach your editor to do it for you. I have a whole bunch of debug shortcuts, but my most used is vardd
which expands to: var_dump(__FILE__ . ':' . __LINE__, $VAR$);die();
显然,在 2018 年,人们仍在回到这个问题上。以上不会是我目前的答案。我会说:教你的编辑为你做这件事。我有一大堆调试快捷方式,但我最常用的是vardd
扩展到:var_dump(__FILE__ . ':' . __LINE__, $VAR$);die();
You can configure this in PHPStorm as a live template.
您可以在 PHPStorm 中将此配置为实时模板。
回答by Gumbo
You can set the second parameter of print_r
to true
to get the output returned rather than directly printed:
您可以设置print_r
to的第二个参数true
来获取返回的输出而不是直接打印:
$output = print_r($myarray, true);
You can use this to fit everything into one echo
(don't forget htmlspecialchars
if you want to print it into HTML):
您可以使用它来将所有内容合二为一echo
(htmlspecialchars
如果您想将其打印为 HTML,请不要忘记):
echo "<pre>", htmlspecialchars(print_r($myarray, true)), "</pre>";
If you then put this into a custom function, it is just as easy as using print_r
:
如果然后将其放入自定义函数中,则就像使用一样简单print_r
:
function printr($a) {
echo "<pre>", htmlspecialchars(print_r($a, true)), "</pre>";
}
回答by James
echo '<pre>' . print_r( $myarray, true ) . '</pre>';
From the PHP.net print_r() docs:
来自 PHP.net print_r() 文档:
When [the second] parameter is set to TRUE, print_r() will return the information rather than print it.
当 [第二个] 参数设置为 TRUE 时,print_r() 将返回信息而不是打印它。
回答by sathia
teach your editor to do it-
教你的编辑去做——
after writing "pr_" tab i get exactly
写完“pr_”标签后,我得到了
print("<pre>");
print_r($);
print("</pre>");
with the cursor just after the $
将光标放在 $ 之后
i did it on textmate by adding this snippet:
我通过添加以下代码段在 textmate 上做到了:
print("<pre>");
print_r($${1:});
print("</pre>");
回答by Nev Stokes
Probably not helpful, but if the array is the only thing that you'll be displaying, you could always set
可能没有帮助,但如果数组是你唯一要显示的东西,你总是可以设置
header('Content-type: text/plain');
回答by ehp
echo "<pre/>"; print_r($array);
回答by NikiC
If you are using XDebug simply use
如果您使用的是 XDebug,只需使用
var_dump($variable);
This will dump the variable like print_r
does - but nicely formatted and in a <pre>
.
这将像print_r
这样转储变量- 但格式很好并且在<pre>
.
(If you don't use XDebug then var_dump
will be as badly formated as print_r
without <pre>
.)
(如果您不使用 XDebug,那么var_dump
格式将与print_r
没有时一样糟糕<pre>
。)
回答by Dan Lugg
Both old and accepted, however, I'll just leave this here:
然而,无论是旧的还是被接受的,我都会把它留在这里:
function dump(){
echo (php_sapi_name() !== 'cli') ? '<pre>' : '';
foreach(func_get_args() as $arg){
echo preg_replace('#\n{2,}#', "\n", print_r($arg, true));
}
echo (php_sapi_name() !== 'cli') ? '</pre>' : '';
}
Takes an arbitrary number of arguments, and wraps each in <pre>
for CGI requests. In CLI requests it skips the <pre>
tag generation for clean output.
接受任意数量的参数,并<pre>
为 CGI 请求包装每个参数。在 CLI 请求中,它会跳过<pre>
标记生成以获得干净的输出。
dump(array('foo'), array('bar', 'zip'));
/*
CGI request CLI request
<pre> Array
Array (
( [0] => foo
[0] => foo )
) Array
</pre> (
<pre> [0] => bar
Array [1] => zip
( )
[0] => bar
[0] => zip
)
</pre>
回答by Paul Z.
I just add function pr() to the global scope of my project. For example, you can define the following function to global.inc (if you have) which will be included into your index.php of your site. Or you can directly define this function at the top of index.php of root directory.
我只是将函数 pr() 添加到我的项目的全局范围内。例如,您可以将以下函数定义到 global.inc(如果有),它将包含在您站点的 index.php 中。或者你也可以直接在根目录的index.php顶部定义这个函数。
function pr($obj)
{
echo "<pre>";
print_r ($obj);
echo "</pre>";
}