Thanks,
Geoff
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
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
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!
>"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.