Save the value of the current variable to a temporary variable, and then assign a unique value to the original variable to find the name of the variable. after finding the name, assign the value of the temporary variable to the original variable PHP. all the variables are stored in the "symbol table" HastTable structure. the scope of the symbol is associated with the activity symbol table. Therefore, there is only one active symbol table at the same time.
You can use the get_defined_vars method to obtain the current active symbol table.
Get_defined_vars // returns an array consisting of all defined variables.
Find the variable name based on the value of the variable, but note that there may be variables with the same value.
Therefore, save the value of the current variable to a temporary variable, and then assign a unique value to the original variable to find the name of the variable, assign the value of the temporary variable to the original variable.
Example 1:
<? Php $ a = '000000'; echo '$ a name is :'. get_variable_name ($ ). 'value :'. $ a; // $ a name is: a value: 100/*** @ param String $ var the variable to be searched * @ param Array $ scope the range to be searched * @ param String variable name */function get_variable_name (& $ var, $ scope = null) {$ scope = null? $ GLOBALS: $ scope; // if there is no range, search for it in globals // because there may be variables with the same value, therefore, save the value of the current variable to a temporary variable, and then assign a unique value to the original variable to find the name of the variable, assign the value of the temporary variable to the original variable $ tmp = $ var; $ var = 'tmp _ value _'. mt_rand (); $ name = array_search ($ var, $ scope, true); // find the variable name based on the value $ var = $ tmp; return $ name;}?>
Example 2: Get the variable name defined in the function
<? Php function test () {$ a = '000000'; echo '$ a name is :'. get_variable_name ($ a) ;}test (); // $ a name is: undefined // The function test2 () cannot be found for the variable globals defined in the function () {$ a = '000000'; echo '$ a name is :'. get_variable_name ($ a, get_defined_vars ();} test2 (); // $ a name is: a // can be found by setting scope to get_defined_vars ()?>