Jump to content

var_dump() function


SaCH

Recommended Posts

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.

 

;)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.........

Link to comment
Share on other sites

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)
  }
}

Link to comment
Share on other sites

@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.

 

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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 !!  :confused:

 

 

Link to comment
Share on other sites

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 ? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.