Is there a way to output all values of a variable or request such as print_r in php? Thanks.
Is there a way to output all values of a variable or request such as print_r in php? Thanks.
Last edited by bluewalrus; 12-13-2010 at 01:59 AM.
Corrections to my coding/thoughts welcome.
No. But the information can be gotten. It depends upon the type of object the variable refers to. A blanket function could be made to test that, with at least one caveat. It also depends upon how you want to see the result.
It would be easier to tell you how to get it for one type of object, where you already know the type. For an array:
Code:<script type="text/javascript"> var ar = ['something', 'anotherthing']; for(var r = 'array ar:\n', i = 0; i < ar.length; ++i){ r += i + ' is ' + ar[i] + '\n'; } alert(r); </script>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
If you're looking for something similar to an associative array in PHP. You can use a for loop to get all properties of an object. For example:
This will give you:Code:<script type="text/javascript"> var post = new Object; post.name = 'Joe'; post.postcode = 'postcode'; post.tel = '92721'; for(i in post) { alert(i + '=' + post[i]); } </script>
name=Joe
postcode=postcode
tel=92721
http://www.quirksmode.org/js/associative.html has a few good examples.
I found this method of doing things particularly useful for me, since I prefer associative arrays![]()
By no means exhaustive or perfect, this will do for a lot of things:
Code:<script type="text/javascript"> function print_r(obj){ function arrayFunc(){ var r = '', i = 0; for(i; i < obj.length; ++i){ r += '[' + i + '] => ' + obj[i] + '\n'; } return r + ')'; } function objectFunc(){ var r = '', p; for(p in obj){ try{ r += '[' + p + '] => ' + obj[p] + '\n'; } catch(e){} } return r + ')'; } var c = obj || obj === 0 || obj === false? obj.constructor : obj; return c === Array? 'Array (\n' + arrayFunc(): c === String? 'String (\n"' + obj.valueOf() + '"\n)' : c === Number? 'Number (\n' + obj.valueOf() +'\n)' : c === Boolean? 'Boolean (\n' + obj + '\n)' : c === Object? 'Object (\n' + objectFunc() : c === Function? 'Function (\n' + obj + '\n)' : 'Object is ' + typeof obj; } alert(print_r(false)); var ar = ['Hello', ['one', 'World']]; alert(print_r(ar)); var obj = {everytime: function(){}, never: "I don't know"}; alert(print_r(obj)); alert(print_r(print_r)); </script>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
bluewalrus (12-12-2010)
print_r() works for associative arrays in PHP, and basically considers numbered indices as associative keys of that number. I'm not sure if Javascript handles it differently, but what you've done in the first couple posts should cover it.
What you just wrote, John, is more like the var_dump() function in PHP that prints all the information contained in any variable, be it an object, array, whatever.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
What I've done is (as far as it goes, like I say - not perfect) almost exactly like PHP's print_r(), which does both associative arrays and those without keys, as well as objects, strings and numbers. It also does booleans - sort of - undocumented, 1 for true, an empty string for false. All I've really added are functions and a sort of catch all to at least try to give a type if javascript can't break down whatever is fed to this function using the function's methods. I only did those because javascript fairly easily can. I don't think PHP does functions at all in that manner. But I'm still just learning PHP, so I wouldn't want to make a negative claim. I can make a positive claim though as I have here about PHP's print_r() because it's all pretty much right there in the manual.
Note: There are no associative arrays in javascript. Well there are, but they're called and technically are objects and use the object syntax, so are constructed with the Object constructor.
Last edited by jscheuer1; 12-12-2010 at 12:31 PM. Reason: clarity
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
As far as I know, the exception is objects. I didn't mean that as a complaint, but just that you'd done a little more than is needed (at least if print_r() really is the goal). var_dump() which handles any type of variable is more useful (in general).
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
The exception of what? From php.net's man page on print_r():
So it does objects (or claims to), and so does the javascript version. I think I have had some experience with PHP's print_r() where it didn't yield much useful information about a particular object. I believe that was because the object itself contained at least one nested object. In the javascript version that too will frustrate your efforts unless the javascript print_r() function is edited to recognize such a situation and deal with it, similar to walking down all descendant nodes in a DOM tree until there are no more child nodes.Return Values
If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.
I'm not sure how or if it's even possible to do that in PHP, though it probably is.
One thing I'm perhaps the least clear on with this concept in PHP is functions. I mean, it appears that you cannot do:
And that doing:PHP Code:
$myFunc = function(){return whatever;};
doesn't seem to make someFunc into something that can be printed. It can be referenced and used in its scope, just not printed out. But I would want to do further testing on that before saying so definitively.PHP Code:
function someFunc(){whatever;}
Last edited by jscheuer1; 12-13-2010 at 01:08 AM. Reason: format - add detail
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
right, no anonymous functions in php. you have to do it in two steps:if you really want to "just do something", then you can just do it (procedurally; aside from being easier to read, there's no *real* advantage to defining a function in php if it's only used once)PHP Code:
function whatever(){
$whatever = // do something
return $whatever;
}
$myFunc = whatever();
Edit:
regarding making a function "into something that can be printed", you have to have (or write) a function that has a return value:PHP Code:
function func_one(){ $var = 'value'; }
print func_one(); // prints nothing (is NULL)
function func_two(){ $var = 'value'; return $var; }
print func_two(); // prints "value"
Last edited by traq; 12-13-2010 at 01:07 AM.
Bookmarks