Use PHP functions in JavaScript

JavaScript print_r

Prints out or returns information about the specified variable

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
function print_r (array, return_val) {
    // Prints out or returns information about the specified variable  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/print_r    // +   original by: Michael White (http://getsprink.com)
    // +   improved by: Ben Bryan
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +      improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // -    depends on: echo
    // *     example 1: print_r(1, true);
    // *     returns 1: 1
    
    var output = "", pad_char = " ", pad_val = 4, d = this.window.document;    var getFuncName = function (fn) {
        var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
        if (!name) {
            return '(Anonymous)';
        }        return name[1];
    };
 
    var repeat_char = function (len, pad_char) {
        var str = "";        for (var i=0; i < len; i++) {
            str += pad_char;
        }
        return str;
    }; 
    var formatArray = function (obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
            cur_depth++;
        } 
        var base_pad = repeat_char(pad_val*cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
        var str = "";
         if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !== 'PHPJS_Resource') {
            str += "Array\n" + base_pad + "(\n";
            for (var key in obj) {
                if (obj[key] instanceof Array) {
                    str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);                } else {
                    str += thick_pad + "["+key+"] => " + obj[key] + "\n";
                }
            }
            str += base_pad + ")\n";        } else if (obj === null || obj === undefined) {
            str = '';
        } else { // for our "resource" class
            str = obj.toString();
        } 
        return str;
    };
 
    output = formatArray(array, 0, pad_val, pad_char); 
    if (return_val !== true) {
        if (d.body) {
            this.echo(output);
        }        else {
            try {
                d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
                this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">'+output+'</pre>');
            }            catch (e) {
                this.echo(output); // Outputting as plain text may work in some plain XML
            }
        }
        return true;    } else {
        return output;
    }
}
external links: original PHP docs | raw js source

Examples

Running

1
print_r(1, true);

Should return

1
1

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have print_r goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Brett Zamir
3 Feb '09 Permalink

q  It can print out a JavaScript object. Since JavaScript has no special associative arrays, this project treats JavaScript objects like associative arrays (that's most likely why it says &quot;array&quot; when you put in a JS object)--and since PHP lets you put in arrays (and objects too), this function should too. var_export() is another option for you too. I hope we can also get var_dump() added at some point, but you should be able to see the contents of your JS objects with print_r or var_export()...

Gravatar
Dennis Day
3 Feb '09 Permalink

q  This code works great for printing out variable information but do you think it would be possible to print out a javascript object? I am new to javascript objects otherwise I would do it myself. To be honest, I'm not even sure it is possible.

Gravatar
Kevin van Zonneveld
31 Dec '08 Permalink

q  @ alexandre: I think your words are very clear :) Thanks alexandre, it's nice of you to let us know. Happy NYE tonight!

Gravatar
alexandre
31 Dec '08 Permalink

q  no words are able to tell how wonderful is your work. Congratulations you all!!!

PS: Sorry about my awful English... I'm Brazillian

Gravatar
Kevin van Zonneveld
1 Dec '08 Permalink

q  @ nikdo: regular for loops won't work for associative arrays (js objects) so we really need these 'for key in array' structures. What version of ie are you using? I find it hard to believe that IE in general does not support these kind of loops at all, because that would have led to problems earlier on.

Gravatar
nikdo
28 Nov '08 Permalink

q  using for(var a in b) isnt a good idea as it doesnt work in ie and is replacable with regular for(e1;e2;e3)

Gravatar
Alexander
19 Nov '08 Permalink

q  May be need change:
[CODE=&quot;Javascript&quot;]
if (obj[key] instanceof Array) {
[/CODE]
Change to:
[CODE=&quot;Javascript&quot;]
if (obj[key] instanceof Array || obj[key] instanceof Object) {
[/CODE]
therefor print a nested objects

Gravatar
Kevin van Zonneveld
6 Oct '08 Permalink

q  @ Tomot: Thanks for sharing, can you tell if it produces errors on any line?

Gravatar
Tomot
3 Oct '08 Permalink

q  print_r(document, true) returns in IE7: &quot;[object]&quot;
in FF3 works fine.

Gravatar
Kevin van Zonneveld
29 Sep '08 Permalink

q  @ Francois: The bug was in the htmlspecialchars_decode function and has been fixed. Sorry for the inconvenience and thank you for tipping me!

Gravatar
Francois
25 Sep '08 Permalink

q  There is a missing coma (,) at line 2985

it is:
= string.replace(/&amp;gt;/g '&gt;');

but we should read:

= string.replace(/&amp;gt;/g, '&gt;');

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @aron budinszky: Thank you very much for your input.
The object vs array story is because PHP does not differentiate between numerically indexed arrays and 'associative arrays'. But as soon as JavaScript encounters an associative array, it becomes an 'Object'. This is an essential difference between JS &amp; PHP. In this project we've chosen to side with PHP.

Hopefully this answers your question. As for the BR tag, if I output PHP's print_r I can choose to enclose it between PRE tags. I would like JS scripters to be able to approach JS's print_r in the same manner.

Gravatar
aron budinszky
7 Aug '08 Permalink

q  also, changing the newlines to &lt;br&gt; and the pad_char to &quot;&amp;nbsp;&quot; creates a more readable result, even if it deviates from the php norm. since print_r is normally used to visually represent data, and since javascript is typically run within a browser, this might be the preferred way in this case...of course the opposite can also be argued...

Gravatar
aron budinszky
7 Aug '08 Permalink

q  oops. there was a mistake in my code below. here's the improved version...(using ben bryan's code). but i noticed that the line 6:
if (obj[key] instanceof Array || obj[key] instanceof Object)
is omitted from the current version in php.js, even though it is included in ben bryan's posted version...

[CODE=&quot;Javascript&quot;]
if (obj instanceof Array || obj instanceof Object) {
if(obj instanceof Array) name = &quot;Array&quot;;
else name = &quot;Object&quot;;
str += name+&quot;\n&quot; + base_pad + &quot;(\n&quot;;
for (var key in obj) {
if (obj[key] instanceof Array || obj[key] instanceof Object) {
str += thick_pad + &quot;[&quot;+key+&quot;] =&gt; &quot;+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
} else {
str += thick_pad + &quot;[&quot;+key+&quot;] =&gt; &quot; + obj[key] + &quot;\n&quot;;
}
}
str += base_pad + &quot;)\n&quot;;
} else {
str = obj.toString();
};
[/CODE]

Gravatar
aron budinszky
7 Aug '08 Permalink

q  here's a bit of an improvement for the print_r function (which, as it seems, ben bryan has partially posted below) to process nested objects as well as arrays. in addition to his code, it is important to print OBJECT or to print ARRAY for the appropriate data type, since accessing the data requires slightly different syntax.

[CODE=&quot;Javascript&quot;]
// {{{ print_r
function print_r( array, return_val ) {
// Prints human-readable information about a variable
//
// + discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_print_r/
// + version: 803.612
// + original by: Michael White (http://crestidg.com)
// * example 1: print_r(['Kevin', 'van', 'Zonneveld']);
// * returns 1: true

var output = &quot;&quot;, pad_char = &quot; &quot;, pad_val = 4;

var formatArray = function (obj, cur_depth, pad_val, pad_char) {
if(cur_depth &gt; 0)
cur_depth++;

var base_pad = repeat_char(pad_val*cur_depth, pad_char);
var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
var str = &quot;&quot;;
var name = &quot;&quot;;

if(obj instanceof Array || obj instanceof Object) {
if(obj instanceof Array) name = &quot;Array&quot;;
else name = &quot;Object&quot;;
str += name+&quot;\n&quot; + base_pad + &quot;(\n&quot;;
for(var key in obj) {
if(obj[key] instanceof Array || obj instanceof Object) {
str += thick_pad + &quot;[&quot;+key+&quot;] =&gt; &quot;+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
} else {
str += thick_pad + &quot;[&quot;+key+&quot;] =&gt; &quot; + obj[key] + &quot;\n&quot;;
}
}
str += base_pad + &quot;)\n&quot;;
} else {
str = obj.toString();
};

return str;
};

var repeat_char = function (len, char) {
var str = &quot;&quot;;
for(var i=0; i &lt; len; i++) { str += char; };
return str;
};

output = formatArray(array, 0, pad_val, pad_char);

if(return_val !== true) {
document.write(&quot;&lt;pre&gt;&quot; + output + &quot;&lt;/pre&gt;&quot;);
return true;
} else {
return output;
}
}// }}}
[/CODE]

Gravatar
Alejandro
4 Jun '08 Permalink

q  Excelent!!!!!!!!!!!

Gravatar
Kevin van Zonneveld
31 May '08 Permalink

q  @ vinnieboombots: Looking at the code, I cannot establish how that could have happened at the moment. Would you be able to provide more debug info? (maybe a codeblock with how you tried to run print_r exactly?) Thank you.

Gravatar
vinnieboombots
26 May '08 Permalink

q  used it on an array of all links on this page (firebug)
it replaced the page contents with a comma separated value string of the array's values.
ie: http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog,http://kevin.vanzonneveld.net/techblog

Php's print_r output is different (&amp; better)

array
[0]=&gt;http://kevin.vanzonneveld.net/techblog
[1]=&gt;http://kevin.vanzonneveld.net/links
[2]=&gt;http://kevin.vanzonneveld.net/code
[3]=&gt;http://kevin.vanzonneveld.net/about

Gravatar
Kevin van Zonneveld
20 May '08 Permalink

q  @ Ben Bryan: Thanks a lot Ben!

Gravatar
Ben Bryan
20 May '08 Permalink

q  Where formatArray function is testing data types, it does not except types of Object, replacing the if statement with the following seems to overcome this. Basically letting OBject types be processed. Tested with FF.
[CODE=&quot;Javascript&quot;]
if (obj instanceof Array || obj instanceof Object) {
str += &quot;Array\n&quot; + base_pad + &quot;(\n&quot;;
for (var key in obj) {
if (obj[key] instanceof Array || obj[key] instanceof Object) {
str += thick_pad + &quot;[&quot;+key+&quot;] =&gt; &quot;+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
} else {
str += thick_pad + &quot;[&quot;+key+&quot;] =&gt; &quot; + obj[key] + &quot;\n&quot;;
}
}
str += base_pad + &quot;)\n&quot;;
} else {
str = obj.toString();
};
[/CODE]

Gravatar
Kevin van Zonneveld
8 May '08 Permalink

q  @ Günter Kits: I do not agree. When the return parameter is not true, the function should not return the string but print it instead. Unless I'm overlooking something, that's what it does now?

Gravatar
Günter Kits
8 May '08 Permalink

q  [CODE=&quot;Javascript&quot;]if(return_val !== true) {[/CODE]
should be
[CODE=&quot;Javascript&quot;]if(return_val == true) {[/CODE]

Gravatar
Kevin van Zonneveld
15 Mar '08 Permalink

q  @ Michael White: replaced!

Gravatar
Michael White
11 Mar '08 Permalink

q  Found a bug in print_r()


So far this bug only seems to affect Netscape and the version I am using is 7.2


Replace this segment:
[CODE=&quot;Javascript&quot;]
var repeat_char = function (len, char) {
var str = &quot;&quot;;
for(var i=0; i &lt; len; i++) { str += char; };
return str;
};
[/CODE]

with this segment:
[CODE=&quot;Javascript&quot;]
var repeat_char = function (len, pad_char) {
var str = &quot;&quot;;
for(var i=0; i &lt; len; i++) { str += pad_char; };
return str;
};
[/CODE]

Netscape thinks that &quot;char&quot; is a reserved word and so cannot be used as a variable name. It errors out saying something about a formal parameter. Changing the 'char&quot; variable to &quot;pad_char&quot; solves that quite easily.


http://crestidg.com

Gravatar
ricardo avalos
11 Mar '08 Permalink

q  Greats Work!!

Very, very, very thanks you!!!


All my cordial greetings and appreciation from Chile

Ricardo
avalos.ricardo@gmail.com

Gravatar
Kevin van Zonneveld
4 Mar '08 Permalink

q  @ Alfonso Jiménez: Great contribution, thank you very much!

Gravatar
Alfonso Jiménez
4 Mar '08 Permalink

q  Hi Kevin! I post here the array_reduce function:

[CODE=&quot;Javascript&quot;]
function array_reduce(a_input, callback) {
var lon = a_input.length;
var res = 0;
var tmp = new Array();

for(i = 0; i &lt; lon; i += 2) {
tmp[0] = a_input[i];
if(a_input[i+1])
tmp[1] = a_input[i+1];
else
tmp[1] = 0;

res += callback.apply(null, tmp);
tmp = new Array();
}

return res;
}
[/CODE]

Usage example:

[CODE=&quot;Javascript&quot;]
array_reduce([1,2,3,4,5], function (x, y) { return (x+y); });
[/CODE]

Regards!
Alfonso Jiménez (http://www.alfonsojimenez.com)


Contribute a New function

More functions

In this category

doubleval
empty
floatval
get_defined_vars
get_resource_type
gettype
import_request_variables
intval
is_array
is_binary
is_bool
is_buffer
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
is_unicode
isset
» print_r
serialize
settype
strval
unserialize
var_dump
var_export

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !