Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting the name of a variable

48 views
Skip to first unread message

Geoff Soper

unread,
May 13, 2004, 6:46:28 PM5/13/04
to
How can I get the name of a variable?
i.e.
$this_is_a_variable = 10;
$variable_name = ????($this_is_a_variable);
echo $variable_name; // Displays 'this_is_a_variable'

Thanks,
Geoff


Andy Hassall

unread,
May 13, 2004, 7:29:31 PM5/13/04
to

You can't.

There'd be no way to pass it to a function, since you pass the value, and the
name of the variable is irrelevant. It would have to be a language construct
tied into the parser; as far as I'm aware there's no such thing.

Why would you need such a construct anyway?

--
Andy Hassall <an...@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

Geoff Soper

unread,
May 13, 2004, 7:37:39 PM5/13/04
to
"Andy Hassall" <an...@andyh.co.uk> wrote in message
news:0018a0dtaafh69dfq...@4ax.com...

> On Thu, 13 May 2004 23:46:28 +0100, "Geoff Soper"
> <geoff.ne...@alphaworks.co.uk> wrote:
>
> >How can I get the name of a variable?
> >i.e.
> >$this_is_a_variable = 10;
> >$variable_name = ????($this_is_a_variable);
> >echo $variable_name; // Displays 'this_is_a_variable'
>
> You can't.
>
> There'd be no way to pass it to a function, since you pass the value, and
the
> name of the variable is irrelevant. It would have to be a language
construct
> tied into the parser; as far as I'm aware there's no such thing.
>
> Why would you need such a construct anyway?

I suspected this might be the case.

I was being lazy and would like a function debug($variable) such as:

$test_variable = 'test string';
debug($test_variable); //prints "$test_variable = test string"

I'll just have to have a function debug($variable_name, $variable) instead!

Thanks,
Geoff


Chung Leong

unread,
May 13, 2004, 9:22:51 PM5/13/04
to
"Geoff Soper" <geoff.ne...@alphaworks.co.uk> wrote in message
news:40a3fac3$0$25327$cc9e...@news-text.dial.pipex.com...

Why, this calls for the power of Bobo the Clown!

function bobo_the_clown($b) {
$bt = debug_backtrace();
extract(array_pop($bt));
$lines = file($file);
$code = implode('', array_slice($lines, $line - 1));
preg_match('/\bbobo_the_clown\s*\(\s*(\S*?)\s*\)/i', $code, $matches);
return @$matches[1];
}

$a = "Hello";

echo bobo_the_clown($a);


Jeremy Shovan

unread,
May 13, 2004, 10:20:20 PM5/13/04
to
Chung Leong wrote:

Very creative, Im impressed

Geoff Soper

unread,
May 14, 2004, 8:21:34 AM5/14/04
to
Jeremy Shovan <jerem...@foundationx.com> wrote in message news:<40A42CE4...@foundationx.com>...

> Chung Leong wrote:
>
> > "Geoff Soper" <geoff.ne...@alphaworks.co.uk> wrote in message
> > news:40a3fac3$0$25327$cc9e...@news-text.dial.pipex.com...
> >
> >>How can I get the name of a variable?
> >>i.e.
> >>$this_is_a_variable = 10;
> >>$variable_name = ????($this_is_a_variable);
> >>echo $variable_name; // Displays 'this_is_a_variable'
> >>
> >
> >
> > Why, this calls for the power of Bobo the Clown!
> >
> > function bobo_the_clown($b) {
> > $bt = debug_backtrace();
> > extract(array_pop($bt));
> > $lines = file($file);
> > $code = implode('', array_slice($lines, $line - 1));
> > preg_match('/\bbobo_the_clown\s*\(\s*(\S*?)\s*\)/i', $code, $matches);
> > return @$matches[1];
> > }
> >
> > $a = "Hello";
> >
> > echo bobo_the_clown($a);
> >
> >
>
> Very creative, Im impressed

Me too! One minor point, I think array_pop should actually be
array_shift for the case of the line where bobo_the_clown is called
being in a file which is included from another file. The file which
contains the call to bobo_the_clown is detailed in the first element
of the backtrace array not the last.

Many thanks, this makes my debugging a lot quicker!

Andy Hassall

unread,
May 14, 2004, 4:12:32 PM5/14/04
to
On Thu, 13 May 2004 21:22:51 -0400, "Chung Leong" <cherny...@hotmail.com>
wrote:

>"Geoff Soper" <geoff.ne...@alphaworks.co.uk> wrote in message
>news:40a3fac3$0$25327$cc9e...@news-text.dial.pipex.com...
>> How can I get the name of a variable?
>> i.e.
>> $this_is_a_variable = 10;
>> $variable_name = ????($this_is_a_variable);
>> echo $variable_name; // Displays 'this_is_a_variable'
>

>Why, this calls for the power of Bobo the Clown!
>
>function bobo_the_clown($b) {
> $bt = debug_backtrace();
> extract(array_pop($bt));
> $lines = file($file);
> $code = implode('', array_slice($lines, $line - 1));
> preg_match('/\bbobo_the_clown\s*\(\s*(\S*?)\s*\)/i', $code, $matches);
> return @$matches[1];
>}
>
>$a = "Hello";
>
>echo bobo_the_clown($a);

Ingenious and cheeky. Nice.

0 new messages