How to get a variable name as a string in PHP? -
say have php code:
$foobar = "a string";
i need function this:
print_var_name($foobar);
which prints:
foobar
any ideas how achieve this? possible in php?
you use get_defined_vars() find name of variable has same value 1 you're trying find name of. not work, since different variables have same values, it's way can think of this.
edit: get_defined_vars() doesn't seem working correctly, returns 'var' because $var used in function itself. $globals seems work i've changed that.
function print_var_name($var) { foreach($globals $var_name => $value) { if ($value === $var) { return $var_name; } } return false; }
edit: clear, there no way in php, because shouldn't have it. there better ways of doing you're trying do.
Comments
Post a Comment