16 May 2018
原文: 如何获取一个变量的名字

真是个骚操作!

function get_variable_name(&$var, $scope = NULL) {
    if (NULL == $scope) {
        $scope = $GLOBALS;
    }

    $tmp  = $var;
    $var  = "tmp_exists_" . mt_rand();
    $name = array_search($var, $scope, TRUE);
    $var  = $tmp;

    return $name;
}

# $test1 = 123;
# $test2 = 123;
# echo get_variable_name($test1) . PHP_EOL;  // test1
# echo get_variable_name($test2) . PHP_EOL;  // test2