niche Posted December 23, 2010 Share Posted December 23, 2010 This script: <?phpsession_start();echo "session: " . var_dump($_SESSION) . '<br/>';?> Displays as: array(1) { ["LAMS_ZV9014-45-127_0_0_1"]=> string(5) "29.45" } session: I expected: session: array(1) { ["LAMS_ZV9014-45-127_0_0_1"]=> string(5) "29.45" }Why am I not getting what I expected? Link to comment Share on other sites More sharing options...
jeffman Posted December 23, 2010 Share Posted December 23, 2010 Weird. What does it look like when you view source? Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 source says:array(1) { ["LAMS_ZV9014-45-127_0_0_1"]=> string(5) "29.45"}session: <br/> Link to comment Share on other sites More sharing options...
Synook Posted December 23, 2010 Share Posted December 23, 2010 var_dump() actually echoes the result to the browser - it doesn't return a string. The statement is evaluated outwards, and so this is like going: $a = var_dump($_SESSION); // var_dump prints the contents of session, a doesn't contain anythingecho "session: " . $a . '<br/>'; // prints "session: <br/>" http://php.net/manual/en/function.var-dump.php Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 I read the reference, but I didn't learn how to get the equivalent of a var_dump() into a variable. I thought Output Control Functions might be the solution, but no luck.How do I the the equivalent of a var_dump() into a variable? Link to comment Share on other sites More sharing options...
Synook Posted December 23, 2010 Share Posted December 23, 2010 Well, you can use output-control functions: <?php session_start(); ob_start(); var_dump($_SESSION); $dump = ob_get_clean(); echo "session: " . $dump . '<br/>';?> Link to comment Share on other sites More sharing options...
birbal Posted December 23, 2010 Share Posted December 23, 2010 i think you can also do this like... $op=print_r($_SESSION,TRUE);echo "Session : $op"; it will return the results as string. which you may catch in any variables. Link to comment Share on other sites More sharing options...
Synook Posted December 23, 2010 Share Posted December 23, 2010 print_r() will return a value if the second parameter is true, but true should be in lowercase. Link to comment Share on other sites More sharing options...
ShadowMage Posted December 23, 2010 Share Posted December 23, 2010 Whenever I need to print out objects or arrays I always use this:echo "<pre>".print_r($ObjOrArr, true)."</pre>";That way, I get a nice formatted string to look at. Link to comment Share on other sites More sharing options...
boen_robot Posted December 23, 2010 Share Posted December 23, 2010 var_dump() can output mutliple arguments, so what I usually do is to simply list them one after the other, like: var_dump('$_SESSION', $_SESSION); The output would be something like: string(9) "$_SESSION"array(1) {["LAMS_ZV9014-45-127_0_0_1"]=>string(5) "29.45"} It's still a little uglier than the ob solution, but it's more trivial to do.I've already forgotten about print_r, as it doesn't give enough information (index and value type) which is more often than not helpful in debugging. Link to comment Share on other sites More sharing options...
thescientist Posted December 23, 2010 Share Posted December 23, 2010 Whenever I need to print out objects or arrays I always use this:echo "<pre>".print_r($ObjOrArr, true)."</pre>";That way, I get a nice formatted string to look at. interesting, I'll have to try that one out. Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 Wow! Ask and ye shall receive. Hadn't heard of Output Control Functions before today. I thought they were what I needed just by what they were called, but I wasn't using ob_start();. Its use made the difference.Thanks for everyone's help. Thanks especially to Deirdre's Dad, Synook, birbal, ShadowMage, boen_robot, and thescientist. There was a lot of learning that went on here especially for me.Niche Link to comment Share on other sites More sharing options...
ShadowMage Posted December 23, 2010 Share Posted December 23, 2010 var_dump() can output mutliple arguments, so what I usually do is to simply list them one after the other, like:var_dump('$_SESSION', $_SESSION); The output would be something like: string(9) "$_SESSION"array(1) {["LAMS_ZV9014-45-127_0_0_1"]=>string(5) "29.45"} It's still a little uglier than the ob solution, but it's more trivial to do.I've already forgotten about print_r, as it doesn't give enough information (index and value type) which is more often than not helpful in debugging. Hmm...Didn't know you could do that with var_dump.print_r actually does show indexes (if by indexes you mean the values used to access array elements, like $arr["index"] )I usually am not concerned with the value type, so it doesn't bother me that it's not shown. Any time I print arrays and objects, I'm only concerned with what the indexes and values are (or if they're even present). Link to comment Share on other sites More sharing options...
boen_robot Posted December 23, 2010 Share Posted December 23, 2010 I meant the type of the index and the type of the value.I've been burned too many times by thinking my index is the number 1 when it's really the string 1. Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 boen_robot, can you use this array to describe what you mean by "I've been burned too many times by thinking my index is the number 1 when it's really the string 1"?Example array:Array ( [0] => Dog [1] => Cat [2] => Horse ) Link to comment Share on other sites More sharing options...
thescientist Posted December 23, 2010 Share Posted December 23, 2010 boen_robot, can you use this array to describe what you mean by "I've been burned too many times by thinking my index is the number 1 when it's really the string 1"?Example array:Array ( [0] => Dog [1] => Cat [2] => Horse ) Array ( ["0"] => Dog ["1"] => Cat ["2"] => Horse ) could be referred to as an associative array Link to comment Share on other sites More sharing options...
ShadowMage Posted December 23, 2010 Share Posted December 23, 2010 I've been burned too many times by thinking my index is the number 1 when it's really the string 1.If it's in quotes, its a string, if not its a number...:)EDIT: Nevermind, I guess it doesn't put quotes around the indexes....coulda swore it did Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 I know, but I don't understand what he meant when he said: "I've been burned too many times by thinking my index is the number 1 when it's really the string 1."Isn't one of the points of this thread, that it's index 1 before the work's done to make it string 1? Link to comment Share on other sites More sharing options...
boen_robot Posted December 23, 2010 Share Posted December 23, 2010 Isn't one of the points of this thread, that it's index 1 before the work's done to make it string 1?Huh?Arrays can have strings or integers as keys. A string can repesent a number. It looks visually the same, and is automatically converted for aritmetic operations. But the string "1" is different from the integer 1... even if the correct value is targeted (and I don't remember if it was... those times are now long gone since my switch to var_dump()), you'll probably get a notice... and I don't like notices (they can be the messenger of security issues to come).If the key type isn't enough to convince you, think of the value types... they can be just as easily confused. Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 That hadn't occured to me. I thought in my example: (Array ( [0] => Dog [1] => Cat [2] => Horse )) the indexes were numeric. If not how do you check? I realize I maybe demonstrating ignorance of a major point of this thread, but like I said this hadn't occured to me before. Link to comment Share on other sites More sharing options...
thescientist Posted December 23, 2010 Share Posted December 23, 2010 I know, but I don't understand what he meant when he said: "I've been burned too many times by thinking my index is the number 1 when it's really the string 1."Isn't one of the points of this thread, that it's index 1 before the work's done to make it string 1?in other words, instead ofif(myArray[1]){ echo myArray[1];}; he should have been testing for if(myArray["1"]){ echo myArray["1"];}; Link to comment Share on other sites More sharing options...
boen_robot Posted December 23, 2010 Share Posted December 23, 2010 You can check if an array key/value is a string or number in one of two ways:1. Use var_dump() instead.2. Loop over the array with the extended foreach ("$array as $key => $value"), and see if is_string($key/$value) and/or is_int($key/$value).When debugging, I prefer 1. Link to comment Share on other sites More sharing options...
niche Posted December 23, 2010 Author Share Posted December 23, 2010 Once again, I'm humbled by learning more of the details.Thanks Again,Niche Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.