You are here: Re: [PHP] Is there a way to get a variable name as a string? « PHP « IT news, forums, messages
Re: [PHP] Is there a way to get a variable name as a string?

Posted by "Richard Lynch" on 07/22/05 11:30

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация