Php method for obtaining the name of a variable

Source: Internet
Author: User
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 ()?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.