PHP Gets the name of a variable, the code explains

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

    1. The first is the parameter &$var , here is quoted, need to notice, the role in the back will say.
    2. Then you use the $GLOBALS hyper-global variable.
    3. 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.

  • 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.