Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Php reference details, php reference transfer use, php reference method return, php Reference analysis! Php reference (& amp;) details 2009-05-1314: 30php reference (that is, add the & amp; symbol before variables, functions, and objects) the reference in PHP means that different names access the same variable content. the pointer is different from the pointer in C language. detailed description of pointer php reference in C language, php reference transfer, php reference method return, php Reference analysis!
Php reference (&)
Php reference (that is, adding & symbol before variables, functions, and objects)
The reference in PHP means that different names access the same variable content.
The pointer in the C language is different from the pointer in the C language. the pointer in the C language stores the address where the variable content is stored in the memory.
PHP reference allows you to use two variables to point to the same content
$ A = "ABC ";
$ B = & $;
Echo $ a; // output here: ABC
Echo $ B; // output here: ABC
$ B = "EFG ";
Echo $ a; // Here, the value of $ a is changed to EFG, So EFG is output.
Echo $ B; // output EFG here
Function address transfer call
I will not talk much about the address transfer call. the following code is provided directly.
Function test (& $)
$ A = $ a + 100;
$ B = 1;
Echo $ B; // output 1
Test ($ B );?? // Here $ B is actually the memory address of the variable content of $ B. you can change the value of $ B by changing the value of $ a in the function.
Echo $ B; // output 101
Note that test (1); will cause an error.
Function Reference return
First look at the code
Function & test ()
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo $ B;
Return $ B;
$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.
$ A = & test (); // This statement outputs the value of $ B to 3.
$ A = 5;
$ A = test (); // This statement outputs a value of 6 for $ B.
The following explains:
In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.
PHP requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the PHP Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.
The example above is as follows:
$ A = test () is used to call a function. it only assigns the value of the function to $ a. any change made to $ a does not affect $ B in the function.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, the value of $ B is changed to 5.
Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.
Var $ abc = "ABC ";
$ B = new;
$ C = $ B;
Echo $ B-> abc; // output ABC here
Echo $ c-> abc; // output ABC here
$ B-> abc = "DEF ";
Echo $ c-> abc; // output DEF here
The above code is the running effect in PHP5
In PHP5, object replication is implemented through reference. In the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B;
In PHP5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone.
Role of reference
If the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then clear it in the form of $ var = null. in other cases, use the default php5 method. in addition, we recommend that you use the "&" method for transferring large arrays in php5 to save memory space.
When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:
$ A = 1;
$ B = & $;
Unset ($ );
Not unset $ B, just $.
When a variable is declared with global $ var, a reference to the global variable is actually created. That is to say, it is the same as doing so:
$ Var = & $ GLOBALS ["var"];
This means that, for example, unset $ var does not unset global variables.
In the method of an object, $ this is always a reference to the object that calls it.
// Next is an episode
In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in php, the reference uses the principle of "copy at Write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.
In layman's terms
1: if the following code exists:
$ A = "ABC ";
$ B = $;
In fact, both $ a and $ B point to the same memory address, not $ a and $ B occupy different memory.
2: Add the following code on the basis of the above code:
$ A = "EFG ";
Because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core automatically determines that $ B will generate a $ a data copy, re-apply for a piece of memory for storage. In short, I can understand this. when you want to use this variable by function for other purposes, you can use value-based transmission, the result is that the passed variable will not change before and after it. when you want to use a function to re-modify or process the variable, it will be passed by reference. after this operation, its variable changes automatically.
Summary: when processing this variable, it is passed by reference. when this variable is used for other purposes, it is passed by value.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.