3

I've been using var_dump() function many times in my scripts to check the actual value and type of the variable. For long time it was enought to make function vardump() witch is defined below:

function vardump($_var){
    echo '<pre>';
    var_dump($_var);
    echo '</pre>';
}

But today I've been using it so many times and places, that I've get confused whitch variable i'm realy checking. So the question is how to determine the name of the value that I'm currently checking?

bumerang
  • 1,776
  • 21
  • 30

5 Answers5

3

After all morning search I've found this solution:

/**
 * Functions that will display name of the variable, and it's value, and type
 * 
 * @param type $_var - variable to check
 * @param type $aDefinedVars - Always define it this way: get_defined_vars()
 * @return type
 */
function vardump(&$_var, &$aDefinedVars = null){
    if($aDefinedVars){
         foreach ($aDefinedVars as $k=>$v)
            $aDefinedVars_0[$k] = $v; 
        $iVarSave = $_var; // now I copy the $_var value to ano
        $_var     = md5(time());

        $aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
        $_var      = $iVarSave;
        $name      = $aDiffKeys[0];
    }else{
        $name = 'variable';
    }

    echo '<pre>';
    echo $name . ': ';
    var_dump($_var);
    echo '</pre>';
}

To get the variable name You have to be sure to use vardump() like below:

vardump($variable_name, get_defined_vars());

get_defined_vars() This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

I'll put some more explanation for the code, there are some exotic functions used in it.

/**
 * Functions that will display name of the variable, and it's value, and type
 * 
 * @param type $_var - variable to check
 * @param type $aDefinedVars - Always define it this way: get_defined_vars()
 * @return type
 */
function vardump(&$_var, &$aDefinedVars = null){
    // $aDefinedVars - is array of all defined variables - thanks to get_defined_vars() function
    if($aDefinedVars){
        // loop below is used to make a copy of the table of all defined variables
        foreach ($aDefinedVars as $k=>$v)
            $aDefinedVars_0[$k] = $v; // this is done like that to remove all references to the variables
        $iVarSave = $_var; // now I copy the $_var value to another variable to prevent loosing it
        $_var     = md5(time()); // completly random value
        // and the most tricky line
        $aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));

Becouse I have change the $_var value array $aDefinedVars_0 and $aDefinedVars are identical except only $_var value(NOTE $aDefinedVars_0 don't use reference to the variables so chengin $_var I didn't affect on $aDefinedVars_0).

Now the array_diff_assoc function compares $aDefinedVars_0 (with oryginal values) against $aDefinedVars (with changed $_var value) and returns the difference with is associative array (array that contains only one position $_var name and $_var value).

The array_keys function returns the keys, numeric and string, from the input array (and input array contains only one position the one that I'm looking for).

        $_var      = $iVarSave; // now I can restore old $_var value
        $name      = $aDiffKeys[0]; // name of the $_var is on the first (and the only position) in the $aDiffKeys array
    }else{ // is someone won't use get_defined_vars()
        $name = 'variable';
    }

    echo '<pre>';
    echo $name . ': ';
    var_dump($_var); // it can be changed by print_r($_var);
    echo '</pre>';
}

PS. I'm sorry for my English.

bumerang
  • 1,776
  • 21
  • 30
  • Yep, this is pretty much the best way to do it. Not recommended(because of the obvious issues (returning 30 null variable names in a script / not working for class properties), but interesting to know. – Vlad Preda Jan 22 '13 at 09:07
  • @VladPreda "best way ... not recommended` --- WAT? – John Dvorak Jan 22 '13 at 09:09
  • Another minor note: If your search fails, chaces are that it's _not_ a variable ;-) – John Dvorak Jan 22 '13 at 09:10
  • 1
    @JanDvorak: What's the problem? It's like having a discussion about the best way to kill your self. You will find it, but you wouldn't recommend anyone to do it :) Let me rephrase: "It's the best way of doing this, but it's not very reliable, so you shouldn't use it)." – Vlad Preda Jan 22 '13 at 09:13
  • 1
    Why in the world would you want to use something like this? If sending a second parameter is an option, why not just send in the variable name? – Hugo Delsing Jan 22 '13 at 09:16
  • 1
    @HugoDelsing the asker is too lazy to change the arguments whenever he copies the call to vardump around ;-) – John Dvorak Jan 22 '13 at 09:20
  • @Jan Dvorak Unfortunately, brutal truth – bumerang Jan 22 '13 at 09:22
  • 1
    Then im glad your not working for me. If your to lazy to debug, I dont even want to know what else your to lazy for :0 – Hugo Delsing Jan 22 '13 at 09:23
  • @HugoDelsing too lazy to collect the wage, hopefully ;-) – John Dvorak Jan 22 '13 at 09:23
  • @Jan Dvorak never too lazy for that :). Actually my problem was more academic than practical – bumerang Jan 22 '13 at 09:36
  • I have to downvote. This should not be the answer a random bycomer sees first and copy-pastes on. – John Dvorak Jan 22 '13 at 09:38
  • For me it work great even with many variables with the same values, but mayby instead of `$_var = !$_var` I sould put some very random value like 'md5(time())'. – bumerang Jan 22 '13 at 09:48
  • @Jan Dvorak too bad You haven't even try my code. Person with Your reputation should know how important it is to double check code and understend it before use. Please check my answer to find the explanation for the code. – bumerang Jan 22 '13 at 14:44
2

I see many strange scripts all over internet to solve your problem or similar ones...

I think, banally, that the best way to do it is passing the name of the variable, shortened if you want, as a string parameter:

function vardump($_var, $var_name){
echo '<pre> Variable <b>$var_name</b>: ';
var_dump($_var);
echo '</pre>';
}

It's not so fast, but it's a good solution.

  • 1
    Actually it's fast (much more faster than my solution), but as I said before, I'm too lazy to put label :) – bumerang Jan 22 '13 at 09:20
  • @bumerang you still have to change the call whenever you copy it around, don't you? – John Dvorak Jan 22 '13 at 09:21
  • @bumerang you could change all occurences in one go using a regex replace. Would you like the regex? – John Dvorak Jan 22 '13 at 09:21
  • @Jan Dvorak Yes it's true I'll ahave to change it - unfortunately ;). I'm not much familiar with regex :( – bumerang Jan 22 '13 at 09:24
  • @bumerang alternatively, you could replace gradually. The method (my version thereof, that is) will work with both signatures `string` and `string, string`, and can be modified to ditch the second argument if it's an array, too. – John Dvorak Jan 22 '13 at 09:29
  • 1
    Bumerang, i think this is the best way...i know, putting labels it's not funny, but i think it's still better than create an expensive, long and strange function to accomplish this task! :-) – ItDeveloper Jan 23 '13 at 12:42
1

Since the argument to vardump could be an expression rather than a variable, this is the closest you can get:

function vardump($_var, $_label){
    echo '<pre>';
    if(!empty($_label)){
      echo $_label . ': ';
    }
    var_dump($_var);
    echo '</pre>';
}
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
1

Well i would like to recommend a php class called dump_r. This class display the information in different colors and it is combination of var_vump and print_r. You can find it on the phpclasses.org

Here is the link

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
0

You cannot get the variable name as a string. You could use the method descriped in How to get a variable name as a string in PHP? but as mentioned, its not perfect.

I think your best bet is to just add it your self or use globals and pass the var name as a strting

function vardump($_var){
    echo '<pre>';
    var_dump($GLOBALS[$_var]);
    echo '</pre>';
}

vardump("foo");
Community
  • 1
  • 1
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • That was the place that I've start with, but I've use the link that was in comment of the accepted answer: http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php – bumerang Jan 22 '13 at 09:29