Since PHP and Javascript are friends:
<?php
function js_dump($js,$n,$z='') {
if($z) {$a = $z . "['" . $n . "']";}
else {$r = 'var '; $a = $n;}
$r .= $a;
if(is_array($js)) {
$r .= ' = new Array();' . "\r\n";
foreach($js as $k => $v) {
if(is_array($v)) {$r .= js_dump($v, $k, $a);}
else {$r .= $a . "['" . $k . "']" . to_js_safe($v);}
}
}
else {$r .= to_js_safe($js);}
return $r;
}
function to_js_safe($js) {return " = '" . addcslashes($js,"\\'") . "';\r\n";}
// example:
$arr = array();
$arr['hello'] = 'ba\'\\"by';
$arr['arr2']['h'] = 'zero';
$arr['arr2']['j'] = 'one';
$arr['arr2']['2']['0'] = 'zero';
$arr['arr2']['2']['1'] = 'one';
$arr[123] = 'but avoid these names in method 1';
// method 1
foreach($arr as $k=>$v) $j .= js_dump($v, $k);
//or method 2 (probably a better idea)
//$j = js_dump($arr,'result');
//then:
//$j = 'AJAX=' . rawurlencode($j);
echo $j;
?>
js_dump( variable/array to dump , starter name )
It's not very optimized and I'm no fan of recursion, but for small arrays this works well. I left 2 "\r\n" in there for readability, you should take them out.
NOTE it doesn't check for numerically named variables.
The result is Javascript eval() friendly.
See this tutorial if you are new to AJAX programming:
http://www.boutell.com/newfaq/creating/ajaxfetch.html
var_dump
説明
void var_dump ( mixed expression [, mixed expression [, ...]] )この関数は、指定した式に関してその型や値を含む構造化された情報を 返します。配列の場合、その構造を表示するために各値について再帰的に 探索されます。
PHP 5 では、オブジェクトのすべての public、private および protected なプロパティが出力されます。
var_dump
sneskid at hotmail dot com
20-Jan-2007 01:48
20-Jan-2007 01:48
eric at ITCbrandt dot com swap ITC/brandt
31-Dec-2006 03:57
31-Dec-2006 03:57
Very new to this and really appreciate the contributions of everyone. I really rely on this site . BigueNique inspired me with the last post.
I am writing an application that is WAY above my head and am buried in pages of variables and objects. I really wanted an easier way to read them.
I had two problems - 1. The lists were too long; 2. It was hard to tell where one ended and the other began; 3. By the time I saw my rendered forms, I forgot where my variable listing was. (Yeah, I can't count, get over it - now you know why this is so hard for me)
I had a variable dumping routine that worked better than the print_r, but elegant_dump was MUCH nicer.
I added a little bit of CSS and javascript to mine, and I have a really quick and easy way to display a colored title with a toggle control to expand or contract the variable structure and contents.
It might be too rudimentary for most of you guys, and I'm not answering anyone's question specifically, but perhaps this creative implementation will make someone's life a little easier. At a minimum, It might be a good example of mixing in the different languages which was initially a big challenge for me.
simply call: dump_e($var,[$TracerMessage],[$ColorBackground],[$ColorContentBox]);
I have explained my functions as well as posted the required code to make it work here: http://tech.brandtitc.com/dump_e.php
It is a little too long to post in this venue.
Best Regards.
BigueNique at yahoo dot ca
03-Nov-2006 01:33
03-Nov-2006 01:33
Geez! That was a hard one!
Wanted a more 'elegant' way do dump variable contents, but soon discovered the 'PHP reference problem', which makes it hard to deal recursive occurence of the same variable.
The only way I found to detect recursion was to actually modify the variable content (passed by reference) in order to be able to know that it was actually parsed.
References in PHP are quite confusing and often PHP got some strange behaviors....
TIP: don't use FOREACH($array as $key->$value) with arrays containing references... You'll get the values the references had at the time they were put in the array, not their actual values. You have to do $value=&$a['key']; (notice the amp) otherwise you're screwed!
Enjoy!
<?php
// An elegant dump
// By BigueNique@yahoo.ca
$elegant_dump_indent = '|  ';
function elegant_dump(&$var, $var_name='', $indent='', $reference='') {
global $elegant_dump_indent;
$reference=$reference.$var_name;
// first check if the variable has already been parsed
$keyvar = 'the_elegant_dump_recursion_protection_scheme';
$keyname = 'referenced_object_name';
if (is_array($var) && isset($var[$keyvar])) {
// the passed variable is already being parsed!
$real_var=&$var[$keyvar];
$real_name=&$var[$keyname];
$type=gettype($real_var);
echo "$indent<b>$var_name</b> (<i>$type</i>) = <font color=\"red\">&$real_name</font><br>".br;
} else {
// we will insert an elegant parser-stopper
$var=array($keyvar=>$var,
$keyname=>$reference);
$avar=&$var[$keyvar];
// do the display
$type=gettype($avar);
// array?
if (is_array($avar)) {
$count=count($avar);
echo "$indent<b>$var_name</b> (<i>$type($count)</i>) {<br>".br;
$keys=array_keys($avar);
foreach($keys as $name) {
$value=&$avar[$name];
elegant_dump($value, "['$name']", $indent.$elegant_dump_indent, $reference);
}
echo "$indent}<br>".br;
} else
// object?
if (is_object($avar)) {
echo "$indent<b>$var_name</b> (<i>$type</i>) {<br>".br;
foreach($avar as $name=>$value) elegant_dump($value, "->$name", $indent.$elegant_dump_indent, $reference);
echo "$indent}<br>".br;
} else
// string?
if (is_string($avar)) echo "$indent<b>$var_name</b> (<i>$type</i>) = \"$avar\"<br>".br;
// any other?
else echo "$indent<b>$var_name</b> (<i>$type</i>) = $avar<br>".br;
$var=$var[$keyvar];
}
}
$a=array();
$b['refers to a']=&$a;
$c['refers to b']=&$b;
$a['refers to c']=&$c;
elegant_dump($a,'$a');
/* Outputs:
$a (array(1)) {
| ['refers to c'] (array(1)) {
| | ['refers to b'] (array(1)) {
| | | ['refers to a'] (array) = &$a <-- stops recursing there
| | }
| }
}
Works with objects too: */
$d->ref2f='';
$e->ref2d=&$d;
$f->ref2e=&$e;
$d->ref2f=&$f;
elegant_dump($d,'$d');
/* Outputs:
$d (object) {
| ->ref2f (object) {
| | ->ref2e (object) {
| | | ->ref2d (object) = &$d
| | }
| }
}
*/
?>
jonbarnett at gmail dot com
03-Oct-2006 04:25
03-Oct-2006 04:25
dumping objects that reference each other could lead to infinite recursion
<?php
$brother = new Sibling();
$sister = new Sibling();
$brother->sister = $sister;
$sister->brother = $brother;
var_dump($brother);
/* dumps all of $brother's properties, including "sister", which dumps all of $sister's properties, including "brother", etc. */
?>
Storm
20-Aug-2006 02:12
20-Aug-2006 02:12
ul_var_dump - dump $var to <ul><li></li></ul>
<?php
function ul_var_dump(&$var,$type=0)
{
if(!$type)
echo "<ul type='circle' style='border:1px solid #a0a0a0;padding-bottom:4px;padding-right:4px'>\n<li>";
if(is_array($var))
{
echo "[array][".count($var)."]";
echo "<ul type='circle' style='border:1px solid #a0a0a0;padding-bottom:4px;padding-right:4px'>\n";
foreach($var as $k=>$v)
{
echo "<li>\"{$k}\"=>";
ul_var_dump(&$v,1);
}
echo "</ul>\n";
}
else
echo "[".gettype($var)."][{$var}]</li>\n";
if(!$type)
echo "</ul>\n";
}
?>
andre at webkr dot de
05-Oct-2005 06:45
05-Oct-2005 06:45
var_dump prefixes the variable type with & if the variable has more than one reference.
This is only true for variables that are part of an array, not for scalar types.
Example:
<?php
$a['foo'] = 'other';
$a['bar'] = 'i_have_ref';
$b =& $a['bar'];
var_dump($a);
var_dump($b);
?>
Result:
array(2) {
["foo"]=>
string(5) "other"
["bar"]=>
&string(10) "i_have_ref"
}
string(10) "i_have_ref"
ospinto at hotmail dot com
07-Aug-2005 01:43
07-Aug-2005 01:43
Just created this neat class that dumps a variable in a colored tabular structure similar to the cfdump tag in Coldfusion. Very easy to use and makes it so much easier to see the contents of variable. For examples and download, visit http://dbug.ospinto.com
edwardzyang at thewritingpot dot com
21-Mar-2005 07:06
21-Mar-2005 07:06
If you're like me and uses var_dump whenever you're debugging, you might find these two "wrapper" functions helpful.
This one automatically adds the PRE tags around the var_dump output so you get nice formatted arrays.
<?php
function var_dump_pre($mixed = null) {
echo '<pre>';
var_dump($mixed);
echo '</pre>';
return null;
}
?>
This one returns the value of var_dump instead of outputting it.
<?php
function var_dump_ret($mixed = null) {
ob_start();
var_dump($mixed);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
?>
Fairly simple functions, but they're infinitely helpful (I use var_dump_pre() almost exclusively now).
anon
28-Jan-2005 11:31
28-Jan-2005 11:31
var_dump(get_defined_vars());
will dump all defined variables to the browser.