发送"var_dump"到FireBug控制台 [英] Sending "var_dump" to FireBug console
问题描述
如您所知,var_dump()
除了显示值外,还显示其数据类型和长度.
As you know var_dump()
in addition to value show its data type and length.
是否可以将其输出记录到 FireBug控制台?
Is there any way to log its output to FireBug console?
我尝试了 FirePHP 和 FireLogger ,但是两者都仅输出变量的值(有时甚至输出不正确的变量值).
I tried FirePHP and FireLogger but both output only value of a variable (sometimes even incorrect variable value).
推荐答案
也许您需要的是这样的东西:
Maybe what you need is something like this:
function var2console($var, $name='', $now=false)
{
if ($var === null) $type = 'NULL';
else if (is_bool ($var)) $type = 'BOOL';
else if (is_string ($var)) $type = 'STRING['.strlen($var).']';
else if (is_int ($var)) $type = 'INT';
else if (is_float ($var)) $type = 'FLOAT';
else if (is_array ($var)) $type = 'ARRAY['.count($var).']';
else if (is_object ($var)) $type = 'OBJECT';
else if (is_resource($var)) $type = 'RESOURCE';
else $type = '???';
if (strlen($name)) {
str2console("$type $name = ".var_export($var, true).';', $now);
} else {
str2console("$type = " .var_export($var, true).';', $now);
}
}
function str2console($str, $now=false)
{
if ($now) {
echo "<script type='text/javascript'>\n";
echo "//<![CDATA[\n";
echo "console.log(", json_encode($str), ");\n";
echo "//]]>\n";
echo "</script>";
} else {
register_shutdown_function('str2console', $str, true);
}
}
用法:var2console($myvar, '$myvar');
或仅var2console($myvar);
几乎没有必要将$now
参数设置为true
,从而导致立即输出<script>
标记.使用 register_shutdown_function()
的好处是您不需要无需注意HTML中的您所在的位置".
It should very rarely be necessary to set the $now
parameter to true
, causing the immediate output of the <script>
tag. The advantage of using register_shutdown_function()
is that you don't need to pay attention to "where you are" in the HTML.
json_encode()
保留传输中的所有字符从PHP到JavaScript.唯一需要注意的是关于编码:json_encode()
仅与UTF-8
一起使用(无论如何,在大多数情况下,这是推荐的编码).您可能需要类似 utf8_encode()
或 mb_convert_encoding()
(如果您使用其他编码(或者,您可以考虑切换到UTF-8
).
The json_encode()
preserves all characters in the transfer from PHP to JavaScript. The only caveat is about encoding: json_encode()
only works with UTF-8
(which is the recommended encoding in most cases, anyway). You may need something like utf8_encode()
or mb_convert_encoding()
if you use a different encoding (or rather, you may consider switching to UTF-8
).
Firebug控制台的输出只是 var_export()
,其后是变量的类型,包括字符串的长度和数组的数量,以及变量的名称(可选).
The output to Firebug's console is simply the output of var_export()
, preceded by the type of the variable, including the length of strings and the count of arrays, and, optionally, by the name of the variable.
var_export()
提供的输出比 var_dump()
一个>.如果您确实需要var_dump()
的输出,则可以使用以下内容:
var_export()
provides a more readable output than var_dump()
. If you really need the output of var_dump()
, you can use something like this:
function dump2console($var, $name='', $now=false)
{
ob_start();
if (strlen($name)) {
echo "$name =\n";
}
var_dump($var);
$str = ob_get_clean();
str2console($str, $now);
}
用法:dump2console($myvar, '$myvar');
或只是dump2console($myvar);
您应避免使用循环引用(var_dump()
检测它们的步骤为时已晚,而var_export()
根本不会检测到它们).例如,对于$GLOBALS
:
You should avoid circular references (var_dump()
detects them a step too late, and var_export()
doesn't detect them at all). This is how to do it, e.g., for $GLOBALS
:
function globals2console($now=false)
{
$g = $GLOBALS;
$g['GLOBALS'] = '(recursion)';
var2console($g, '$GLOBALS', $now);
}
这篇关于发送"var_dump"到FireBug控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!