Re: Is there a way to get a variable name as a string?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, July 18, 2005 8:24 pm, Ryan A said:
> I didnt totally understand you q in the beginning (and still dont fully),
> but....

<?php
  $x = 5;
?>

He wants a function that, if you put in $x, you get out 'x'

For *ANY* $variable.

There is no such function.

Usually the person asking it is doing something very newbie-ish, and very
wrong.

I know, cuz I was that person, many many years ago. :-)

I think what I needed that time was "variable variables"...

The other thing that tends to bring up this question is somebody trying to
write a custom error handler, not knowing about
http://php.net/trigger_error and friends.  Though that only gets you the
line number and filename where the error occured, not a specific variable
name.

If for some reason one *NEEDS* to have "names" and variables and to know
what a variable's "name" is, you *probably* want to use an associative
array and use the "keys" as your names.

Hopefully this is helpful to the Original Poster, or somebody some day.

PS

It's true that your variable could/would/should appear in debug_backtrace,
but how would you pick it out from all the other variables that would
appear in your debug_backtrace?

For that matter, it's in $_GLOBALS, but how would you pick it out?

You could print out all variables that were equal to your variable, and
have a list of candidates...

<?php
  $x = 5;
  $y = 3;
  $z = 5;
  while (list($k, $v) = each($GLOBALS)){
    if ($v === $x) echo "$k might be your variable...<br />\n";
  }
?>

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux