SaCH Posted March 18, 2012 Share Posted March 18, 2012 Hello guys, i had seen the function var_dump() is used in many coding but even i don't know about the function. Can anybody explain what is the purpose of var_dump() function ? if you can explain it with examples then it will be more easy to save it my memory. Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/ Share on other sites More sharing options...
kicken Posted March 18, 2012 Share Posted March 18, 2012 See the manual page for details and examples. var_dump pro tip: if you come across a function you've never seen before and want details, just go to http://www.php.net/<function name here>, eg http://www.php.net/var_dump That will pull up the manual page for the function (if the function is part of PHP itself, and not something custom made). Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328729 Share on other sites More sharing options...
trq Posted March 18, 2012 Share Posted March 18, 2012 It is used for debugging mostly and does what the name suggests. It dumps (displays) data about a variable. Try it an see. var_dump(array('foo' => 'bar', 1, 4, 'x' => 'y', 'obj' => new StdClass)); Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328730 Share on other sites More sharing options...
requinix Posted March 18, 2012 Share Posted March 18, 2012 A big part of what it does is show types: if you print_r($x) and get "1" you won't know if that's a number or string. For a fun example of why that can be useful, var_dump(array_keys(array("0" => "zero"))); Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328738 Share on other sites More sharing options...
SaCH Posted March 18, 2012 Author Share Posted March 18, 2012 Got it ! var_dump() is used to get the details such as Data Types,its value also it will return the length of string in a variable. The function is very useful to know more details by a small code. Means If we consider this code $val = "SacH"; echo var_dump($val); the output will be string(4) "SaCH" The result explain that - The variable contains a string value. - The value of variable is "SaCH" - The length of the string is 4 Thanks For Assistance......... Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328797 Share on other sites More sharing options...
scootstah Posted March 19, 2012 Share Posted March 19, 2012 You don't actually need to echo it, it will output on its own. Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328910 Share on other sites More sharing options...
SaCH Posted March 19, 2012 Author Share Posted March 19, 2012 You don't actually need to echo it, it will output on its own. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328927 Share on other sites More sharing options...
scootstah Posted March 19, 2012 Share Posted March 19, 2012 I just wanted to point this out, because I tend to use it a lot for debugging. With print_r you can add a second parameter to return the output instead of echoing it. Therefore you can use it in <pre> tags to make it look pretty. Like, if you have big arrays or objects you will be able to have one item per line instead of all garbled together. With var_dump you can't do that. Instead, though, you can capture the output in a buffer and then do the same thing. Something like this: function var_dump_pre() { ob_start(); call_user_func_array('var_dump', func_get_args()); return '<pre>' . ob_get_clean() . '</pre>'; } $arr = array(range(0,5)); echo var_dump_pre($arr); Which changes the output from: array(1) { [0]=> array(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) } } to array(1) { [0]=> array(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) } } Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328997 Share on other sites More sharing options...
trq Posted March 19, 2012 Share Posted March 19, 2012 Or you could just use var_export. echo '<pre>' . var_export($var, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328998 Share on other sites More sharing options...
scootstah Posted March 19, 2012 Share Posted March 19, 2012 Well, that's boring... Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1328999 Share on other sites More sharing options...
trq Posted March 19, 2012 Share Posted March 19, 2012 It's not really the same output either. Similar. Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329005 Share on other sites More sharing options...
SaCH Posted March 19, 2012 Author Share Posted March 19, 2012 @scootstah Sir, Consider the above code & its output. <?php $arr = array(range(0,5)); echo '<pre>'; var_dump($arr); echo '</pre>'; ?> Output : array(1) { [0]=> array(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) } } Then what is the difference between var_dump() and print_r() using with the <pre> tag ? I tried the <pre> tag with both function var_dump() and print_r() and i got the same output. Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329030 Share on other sites More sharing options...
Muddy_Funster Posted March 19, 2012 Share Posted March 19, 2012 ok, try it like this: <?php echo "var_dump:<br>"; $name = array('muddy', 'funster', 'frogger', 'packman'); $output = "<pre>".var_dump($name)."</pre>"; print $output; echo "<br><br><br>print_r:<br>"; $name = array('muddy', 'funster', 'frogger', 'packman'); $output = "<pre>".print_r($name, true)."</pre>"; print $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329040 Share on other sites More sharing options...
SaCH Posted March 19, 2012 Author Share Posted March 19, 2012 ok, try it like this: <?php echo "var_dump:<br>"; $name = array('muddy', 'funster', 'frogger', 'packman'); $output = "<pre>".var_dump($name)."</pre>"; print $output; echo "<br><br><br>print_r:<br>"; $name = array('muddy', 'funster', 'frogger', 'packman'); $output = "<pre>".print_r($name, true)."</pre>"; print $output; ?> Yes with your example the output will be similar to the "scootstah" opinion. But with my example its displaying the same output for both function. Why it happen & scootstah opinion is right or anything else ? Just try with this code & see the output. <?php echo "var_dump:<br>"; $name = array('muddy', 'funster', 'frogger', 'packman'); echo '<pre>'; var_dump($name); echo '</pre>'; echo "<br><br><br>print_r:<br>"; $name = array('muddy', 'funster', 'frogger', 'packman'); echo '<pre>'; print_r($name); echo '</pre>'; ?> Confused !! Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329045 Share on other sites More sharing options...
Muddy_Funster Posted March 19, 2012 Share Posted March 19, 2012 Your way works because you have completly detatched the use of <pre> from the return function that you are calling. Your echoing the <pre> tags as totaly indipendant entities, you could, in theory, do this with anything. Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329069 Share on other sites More sharing options...
SaCH Posted March 19, 2012 Author Share Posted March 19, 2012 Your way works because you have completly detatched the use of <pre> from the return function that you are calling. Your echoing the <pre> tags as totaly indipendant entities, you could, in theory, do this with anything. Ok.. then what is the problem with my way ? Is there is any problem if i return the function with completely detached use of of <pre> tag ? Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329077 Share on other sites More sharing options...
Muddy_Funster Posted March 19, 2012 Share Posted March 19, 2012 nope, your just circumventing the point that scooshta was making, and efectivly performing what his function does manualy and without the use of the output buffer. Quote Link to comment https://forums.phpfreaks.com/topic/259196-var_dump-function/#findComment-1329081 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.