1

Possible Duplicate:
How to get a variable name as a string in PHP?

I'm needding something similar to the code below:

code

<?php

$var1='my var 1';

$array1['1']='first position of my array 1';
$array1['2']='second position of my array 1';

echo name($var1);

echo "<br>";

echo name($array1);

function name($parameter){
...?...
}
?>

screen return

var1
array1

So, I need to write a function that gives me the name of a variable or an array that I pass to it. How can I perform this?

I will use this function on another function to perform some tasks to me...

Community
  • 1
  • 1
GarouDan
  • 3,743
  • 9
  • 49
  • 75
  • Trevor, this solution dont works to my problem. – GarouDan Sep 28 '12 at 19:27
  • This links only works for variables, not arrays. With arrays crashes. – GarouDan Sep 28 '12 at 19:27
  • @GarouDan Please provide an example where you would need `echo name($array1)` and `echo 'array1'` won't do. – lanzz Sep 28 '12 at 19:28
  • this link is the same of the others, and crashes to arrays. – GarouDan Sep 28 '12 at 19:31
  • you may want to try: http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php – MagePal Extensions Sep 28 '12 at 19:31
  • Could you explain why you're trying to do this? It seems like a bad idea to be dynamically getting variables unless there's soem really specific reason. – dudewad Sep 28 '12 at 19:31
  • @lanzz, a use for this is I'm writing my own debugger file or class. I will include this independent file on the system that I'm working. So I would like something similar to legible($variable), that outputs an array or a variable with his name, a good format and then exit. I'm justing needing fix the arrays part with a good way. – GarouDan Sep 28 '12 at 19:32
  • Check the other answer on the duplicate link, not just the accepted one. One of them points to [this solution](http://www.php.net/manual/en/language.variables.php#49997), which might work for you. – bfavaretto Sep 28 '12 at 19:34
  • I will do the test. Please wait a moment. – GarouDan Sep 28 '12 at 19:35
  • 1
    That won't be possible the way you think about it. Figuring the name of the variable that has been passed to a function is like figuring the text on the label of a bottle by examining the contents of the glass it has been poured into. – lanzz Sep 28 '12 at 19:35
  • lol. The truth it is possible. But looks like with no easy way. – GarouDan Sep 28 '12 at 19:37
  • This might be useful for what you're doing: http://php.net/manual/en/function.debug-backtrace.php – bfavaretto Sep 28 '12 at 19:43
  • Bad design smell coming - use [`debug_backtrace`](http://php.net/manual/en/function.debug-backtrace.php) for debugging. – moonwave99 Sep 28 '12 at 19:47
  • huhuh. Looks like an interesting function. I will do some code with it. Thx. – GarouDan Sep 28 '12 at 19:50

2 Answers2

1

As @bfavaretto said,

this worked fine to me:

<?php

$var1='my var 1';

$array1['1']='first position of my array 1';
$array1['2']='second position of my array 1';

echo name($var1);

echo "<br>";

echo name($array1);

function name(&$var, $scope=false, $prefix='unique', $suffix='value'){
    if($scope) $vals = $scope;
    else      $vals = $GLOBALS;
    $old = $var;
    $var = $new = $prefix.rand().$suffix;
    $vname = FALSE;
    foreach($vals as $key => $val) {
      if($val === $new) $vname = $key;
    }
    $var = $old;
    return $vname;
}
?>
GarouDan
  • 3,743
  • 9
  • 49
  • 75
0

try modifying the function in the following way (untested)

function print_var_name($var) { 
    $left=var_export($var, true);
    foreach($GLOBALS as $var_name => $value) { 
        $right=var_export($value, true);
        if ($left === $right) { 
             return $var_name; 
        } 
    } 
    return false; 
} 
Dave
  • 991
  • 1
  • 7
  • 15
  • note that you are dumping the contents of both arrays in this case and comparing the results. – Dave Sep 28 '12 at 19:42
  • a big risk of this is if you have two variables with the same value, you could get returned the wrong one, so accuracy is a *real* issues, but I think this is as close as you are going to get to what you are trying for. – Dave Sep 28 '12 at 19:44