By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,027 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,027 IT Pros & Developers. It's quick & easy.

Getting the name of a variable

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Thu, 13 May 2004 23:46:28 +0100, "Geoff Soper"
<ge***************@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?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:00********************************@4ax.com...
On Thu, 13 May 2004 23:46:28 +0100, "Geoff Soper"
<ge***************@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
Jul 17 '05 #3

P: n/a
"Geoff Soper" <ge***************@alphaworks.co.uk> wrote in message
news:40***********************@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'

Thanks,
Geoff


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);
Jul 17 '05 #4

P: n/a
Chung Leong wrote:
"Geoff Soper" <ge***************@alphaworks.co.uk> wrote in message
news:40***********************@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'

Thanks,
Geoff

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
Jul 17 '05 #5

P: n/a
Jeremy Shovan <je********@foundationx.com> wrote in message news:<40**************@foundationx.com>...
Chung Leong wrote:
"Geoff Soper" <ge***************@alphaworks.co.uk> wrote in message
news:40***********************@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!
Jul 17 '05 #6

P: n/a
On Thu, 13 May 2004 21:22:51 -0400, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Geoff Soper" <ge***************@alphaworks.co.uk> wrote in message
news:40***********************@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.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.