How to get the name of a variable

Source: Internet
Author: User

For example, I provide a query service where users can submit a person's name and age as a query condition.

Suppose I want to query a name called "Laruence", age is 27 people, I think this person's definition of the query token can be written:

Laruence=27
Unfortunately, when such a token as a processing script submitted to the server by query string, you'll find, eh, I don't know what the username is,,,

Well, then, you have to write this:

Username=laruence&age=27
So, can you get the name of a variable?

First of all, from the possibility analysis,

We know that in the C language, all symbols are "replaced" in the compiler.

In PHP, all variables are stored in a hasttable structure called a "symbol table." In the process of parsing execution, "symbolic" information is still retained, so it is definitely accessible.

In PHP, the scope of the symbol is associated with the active symbol table. At the same time, there is only one active symbol table.

So how do you understand the active symbol table and the symbol table?

For PHP, the currently active symbol table is stored in the global variable, eg (active_symbol_table), and at the same time, there is a global symbol table stored in eg (symbol_table), before entering the execution body of a function call, Generates a new active_symbol_table and maintains a symbolic table stack of call stack styles: EG (Symtable_cache) so that the active symbol table (scope) is restored before the function call is exited.

At the same time in PHP, scope inheritance cannot be implemented, that is, symbols that do not directly access the extraterritorial layer (need to be added with the Golbal declaration), and if the declaration of global is added, a copy is generated in the current active scope, that is to say, Symbols that do not exist visible in the current scope are saved in the global symbol table.

As above analysis, we just need to find the name of the variable we need in the current active symbol table.

Of course, with these not enough, how do we get the current symbol table in the PHP script?

Get_defined_vars

One problem to note, however, is that Get_defined_vars returns the variable name defined in the current active symbol table, that is, if you need to wrap a function like this:

Get_variable_name ($var)
And it is not feasible to try to get the symbol table in the call Get_variable_name time by Get_defined_vars in this function.

So, the function that we get the variable name should be the following:

Get_variable_name ($var, $scope)
Now that you've got the current active symbol table, how do you get the name of the variable?

Obviously, we need to query the table based on the value of the variable, and find the variable with the value equal to the value, but there is another problem, that is, there might be multiple variables with equal values?

So, we need to give this variable a unique value. And if you want this variable, then we have to change the form of this objective function:

Get_variable_name (& $var, $scope)
Next, perfect the function body:

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;
}
Also, one problem is that if there is a reference between multiple variables, the function simply returns the first defined variable name ...

In addition, you can also refer to: http://php.net/manual/en/language.variables.php







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.