Php: how to get the name of a variable; php: how to get the variable _ PHP Tutorial

Source: Internet
Author: User
Php is used to obtain the name of a variable. php is used to obtain the variable name. Php obtains the name of a variable. in php, all the variables are stored in the HastTable structure of the symbol table, the scope of a symbol is related to the activity symbol table. php can get the name of a variable. php can get the variable.

In PHP, all variables are stored in the "symbol table" HastTable structure, and 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 ()?>


How to obtain the variable name in PHP

Function vname (& $ var, $ scope = false, $ prefix = 'unique', $ suffix = 'value ')
{
If ($ scope) $ vals = $ scope;
Else $ vals = $ GLOBALS;
$ Old = $ var;
$ Var = $ new = $ prefix. rand (). $ suffix;
$ Vname = FALSE;
Foreach ($ vals as $ key => $ val ){
If ($ val ==$ new) $ vname = $ key;
}
$ Var = $ old;
Return $ vname;
}

Echo "\ $ variable name =". vname ($ variable name );
?>

Output result: $ variable name = variable name

How does php obtain the variables of another file from one file?

1. Upstairs methods include ("A. php") or require ("A. php ")
2. the variable is appended with the url and obtained using $ _ GET ['variable name'] in B. php.
3. click it to set this variable to session or cookie.

In PHP, all variables are stored in the "symbol table" HastTable structure, and the scope of the symbol is associated with the activity symbol table...

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.