function get_variable_name($var) {
$trace = debug_backtrace();
$this_file_source_code = file( __FILE__ );
$the_line_code_that_call_to_this_function = $this_file_source_code[$trace[0]['line'] - 1];
// get function argument - the $var name
preg_match( "#\\$(\w+)#", $the_line_code_that_call_to_this_function, $match);
return $match;
}
$num = 23452;
print_r(get_variable_name($num));
$str = "PHP";
print_r(get_variable_name($str));
$arr = array(1, 2, 3);
print_r(get_variable_name($arr));
/*
run:
Array
(
[0] => $num
[1] => num
)
Array
(
[0] => $str
[1] => str
)
Array
(
[0] => $arr
[1] => arr
)
*/