Here is a javscript version of print_r() i had to throw together recently. I was getting so used to being able to use it with php that I became frustrated there was not a javascript version of the like.
I'm playing with javascript remote scripting to a php server page. (The technique the makes maps.google.com so cool and smooth). And was frustrated that techniques used in my php development were so different from the javascript ones. The JS debugger catches syntax errors, but doesn't help me figure out whats going on. So the relevance of this to PHP, there is some, kinda, alittle.
This works fine for what I need it for. But of course, critique away!
JAVASCRIPT:
function print_r(input, _indent)
{
var indent = (typeof(_indent)=='string')?_indent+' ':' ';
var paren_indent = (typeof(_indent)=='string')?_indent+' ':'';
if ( typeof(input) == 'string' ) {
var output = "'"+ input +"'\n"
} else if ( typeof(input) == 'boolean' ) {
var output = (input?'true':'false') +"\n"
} else if ( typeof(input) == 'object' ) {
var output = ((input.reverse)?'Array':'Object') +"\n"
output += paren_indent + "(\n";
for ( var i in input ) {
output += indent + "["+ i +"] => "+ print_r(input[i],indent)
}
output += paren_indent + ")\n"
}
return output
}