Name of a Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Name of a Variable

Post by anjanesh »

Code: Select all

$abc = '55x';
echo foo($abc)
should output

Code: Select all

abc
I want to know the name of the variable.
Is this possible ?
Its possible to extract the classname using var_export but not a variable name.

Code: Select all

class clsA { public $var; }
$a = new clsA();
$a->var = 5;
$b = var_export($a, TRUE);
echo substr($b, 0, strpos($b, "::"));
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Name of a Variable

Post by RobertGonzalez »

There have been several code snippets published on our forums that can fetch a variable name (I posted one of them). If not, look in the user comments on the print_r() page of the manual. Several developers have posted tips on how to do what you are after.
Post Reply