Is There an Equivalent For Var_Dump (PHP) in JavaScript

What is the JavaScript equivalent of var_dump or print_r in PHP?

Most modern browsers have a console in their developer tools, useful for this sort of debugging.

console.log(myvar);

Then you will get a nicely mapped out interface of the object/whatever in the console.

Check out the console documentation for more details.

Is there an equivalent for var_dump (PHP) in Javascript?

As the others said, you can use Firebug, and that will sort you out no worries on Firefox. Chrome & Safari both have a built-in developer console which has an almost identical interface to Firebug's console, so your code should be portable across those browsers. For other browsers, there's Firebug Lite.

If Firebug isn't an option for you, then try this simple script:

function dump(obj) {
var out = '';
for (var i in obj) {
out += i + ": " + obj[i] + "\n";
}

alert(out);

// or, if you wanted to avoid alerts...

var pre = document.createElement('pre');
pre.innerHTML = out;
document.body.appendChild(pre)
}

I'd recommend against alerting each individual property: some objects have a LOT of properties and you'll be there all day clicking "OK", "OK", "OK", "O... dammit that was the property I was looking for".

var_dump (PHP) equivalent for node.js

To get what you’d get in the console by using console.log as a string for sending to the client as part of your response, you can use util.inspect.

"use strict";

const http = require("http");
const util = require("util");

http.createServer((request, response) => {
response.setHeader("Content-Type", "text/plain;charset=utf-8");
response.end(util.inspect(request));
}).listen(8000, "::1");

Is there a print_r or var_dump equivalent in PHP that does not echo the result by default?

Why not create a custom function that will wrap all your print_r calls? I use something like this:

function good_print() {
$log = '';
foreach(func_get_args() as $arg) $log .= print_r($arg, true);
return $log;
}

It saves time, gives me better functionality, plus I don't have to worry about "did I use the right call this time?"

Quick Look at Objects in Rhino Shell (PHP var_dump equivalent?)

obj.toSource() will do for basic inspection.

Is there an equivalent for var_dump (PHP) in Javascript?

As the others said, you can use Firebug, and that will sort you out no worries on Firefox. Chrome & Safari both have a built-in developer console which has an almost identical interface to Firebug's console, so your code should be portable across those browsers. For other browsers, there's Firebug Lite.

If Firebug isn't an option for you, then try this simple script:

function dump(obj) {
var out = '';
for (var i in obj) {
out += i + ": " + obj[i] + "\n";
}

alert(out);

// or, if you wanted to avoid alerts...

var pre = document.createElement('pre');
pre.innerHTML = out;
document.body.appendChild(pre)
}

I'd recommend against alerting each individual property: some objects have a LOT of properties and you'll be there all day clicking "OK", "OK", "OK", "O... dammit that was the property I was looking for".

Make var_dump look pretty

I really love var_export(). If you like copy/paste-able code, try:

echo '<pre>' . var_export($data, true) . '</pre>';

Or even something like this for color syntax highlighting:

highlight_string("<?php\n\$data =\n" . var_export($data, true) . ";\n?>");

Reusable function:

function highlight_array($array, $name = 'var') {
highlight_string("<?php\n\$$name =\n" . var_export($array, true) . ";\n?>");
}

You can do the same with print_r(). For var_dump() you would just need to add the <pre> tags:

echo '<pre>';
var_dump($data);
echo '</pre>';

Javascript empty array and empty object equivalent in PHP

The first one is an Array of one element, being the element an empty string. In PHP it'd be [""] or array("").

<?php
$a = [""];
var_dump($a); // array(1) { [0]=> string(0) "" }

$b = array("");
var_dump($b); // array(1) { [0]=> string(0) "" }

The second is an object with empty string value for key 0. In PHP it could be represented as: (object)[""] or (object)[0 => ""].

<?php
$a = (object)[""];
var_dump($a); // object(stdClass)#1 (1) { [0]=> string(0) "" }

$b = (object)[0=>""];
var_dump($b); // object(stdClass)#2 (1) { [0]=> string(0) "" }

// Also:

$c = new \stdClass;
$c->{0} = "";
var_dump($c); // object(stdClass)#3 (1) { ["0"]=> string(0) "" }

Please, read about PHP Arrays and PHP Objects.



Related Topics



Leave a reply



Submit