Hello, in PHP, I want to get the name of a variable, like
$name = 'xiaomin';echo get_varible_name($name);\\ output 'name'
I see a code here
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;}
What does that $var = "tmp_exists_" . mt_rand();
mean? Who can explain
Reply content:
Hello, in PHP, I want to get the name of a variable, like
$name = 'xiaomin';echo get_varible_name($name);\\ output 'name'
I see a code here
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;}
What does that $var = "tmp_exists_" . mt_rand();
mean? Who can explain
The master carefully read the bird brother's original words will find this sentence:
Obviously, we need to query the table based on the value of the variable, find the value equal to the value of the variable, but there is another problem, that is, there may be more than one variable value equal AH?
Let's Test it:
1. Comment out the words that the Lord doubts:
$test1 = 123;$test2 = 123;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;}echo get_variable_name($test1)."\n";echo get_variable_name($test2);
Let's look at the output:
Cannot distinguish
2. Now we uncomment:
$test1 = 123;$test2 = 123;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;}echo get_variable_name($test1)."\n";echo get_variable_name($test2);
The output is as follows:
Differentiate success
The first answer to the question of the main problem is to modify the value of the variable that needs to be found to be unique in the global variable. And then say my understanding of this function:
There are several places to be aware of this function:
- The first is the parameter
&$var
, here is quoted, need to notice, the role in the back will say.
- Then you use the
$GLOBALS
hyper-global variable.
- That's what the Lord says. Change the
$var
value of a variable
$GLOBALS a hyper-global variable is an array that is responsible for storing all the variables on the page, which means searching for the value of the variable in the global variable array using the Array_search () function, and returning its key name if it exists. to prevent duplicate values from appearing, the function uses the Mt_rand () function $var
to change the value to a unique random variable and to re-assign the original value after the query ends , and if there is no such sentence, it cannot be distinguished if there is a variable with the same value.
The first argument that comes in is a reference to a variable that can be passed outside the method's internal operation.
Immediately after the operation, the outside variable is assigned a random, non-repeating temporary value that is conveniently located in the scope (default GLOBAL) to be searched by value.
Once found, assign the value of the variable back.
The key to the method is the use of Array_search, search by value (so to give a value that cannot be duplicated), and the Return key name (the variable under GLOBAL scope exists in $GLOBAL [$key] = $value this array).
The function takes advantage of the variable concept of PHP variables.