In php, you only need to add & amp; before the original object to assign values by reference, $ a & amp; $ B; in fact, the reference in php is that two variables with different names point to the same value. Reference what is in PH
In php, you only need to add one & before the original object to assign values by reference, $ a = & $ B; in fact, the reference in php is that two variables with different names point to the same value.
Reference
Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system.
I. variable reference,The code is as follows:
-
- $ A = 100;
- $ B = & $;
- Echo $ B; // output 100
- Echo $ a; // output 100 here, indicating that the values of $ a and $ B are both one hundred.
- $ B = 200;
- Echo $ a; // output 200
- Echo $ B; // output 200 here, which means they use the same address. Change one, and the other will also change.
- ?>
2. reference and pass the value in the function,The code is as follows:
-
- Function main ($ a, $ B ){
- $ B = $ a + 100;
- Return $ B;
- }
- Main (55, & $ B); // Here $ B is actually passing its memory address to the $ B parameter in the main function, the value of $ B is changed by changing the parameter $ B.
- Echo $ B; // The output is 155,
- ?>
3. pass the reference value of the object,The code is as follows:
-
- Class club {
- Var $ name = "real madrid ";
- }
- $ B = new club;
- $ C = $ B;
- Echo $ B-> name; // output real madrid
- Echo $ c-> name; // output real madrid
- $ B-> name = "ronaldo ";
- Echo $ c-> name; // output ronaldo
- ?>
Cancel reference:When you unset a reference, you just disconnect the binding between the variable name and the variable content, which does not mean that the variable content is destroyed, for example, the following code:
-
- $ A = 'Ronaldo'
- $ B = & $;
- Unset ($ );
- ?>
It won't unset $ B, just $ a, for example, reference transfer, test1.php, the code is as follows:
-
- /**
- * Reference transfer
- The following content can be passed through reference:
- Variable, such as foo ($)
- New statement, such as foo (new foobar ())
- Reference returned from the function, for example:
- */
- Function foo (& $ var)
- {
- $ Var ++;
- }
- $ A = 5;
- // Valid
- Foo ($ );
- Foo (new stdClass ());
- // Illegal use
- Function bar () // Note the missing &
- {
- $ A = 5;
- Return $;
- }
- Foo (bar (); // fatal error caused by PHP 5.0.5
- Foo ($ a = 5) // expression, not a variable
- Foo (5) // cause a fatal error
- ?>
The code for test2.php is as follows:
-
- Function test (& $)
- {
- $ A = $ a + 100;
- }
- $ B = 1;
- Echo $ B; // output 1
- Test ($ B); // here, $ B actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function.
- Echo"
";
- Echo $ B; // output 101
- /*****************************
- *
- * Note that the parameter after call_user_func_array is required &
- *
- *****************************/
- // Do not add the & symbol before $ B in the above "test ($ B);", but in the function "call_user_func_array", to reference the parameter passing, the & symbol is required, as shown in the following code:
- Function a (& $ B ){
- $ B ++;
- }
- $ C = 0;
- Call_user_func_array ('A', array (& $ c ));
- Echo $ c;
- // Output 1
- ?>
Reference return
When you want to use a function to find the variable to which the reference should be bound, do not use the return reference to increase performance. the engine is smart enough to optimize it, A reference is returned only when there are reasonable technical reasons! To return a reference, use the following syntax:
-
- 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 as 3. here, the memory address of the $ B variable in return $ B is pointed to the same place as the memory address of the $ a variable.
- $ A = 5; // The value of the $ B variable in return $ B has been changed.
- $ 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 it for half a day
The example above is as follows: $ a = test () is used to call a function. it only assigns the value of the function to $, $ a does not affect $ B in the function, but calls the function in the $ a = & test () method, the function is to direct the memory address of the $ B variable in return $ B to the memory address of the $ a variable, that is, the equivalent effect ($ a = & $ B;) is generated. Therefore, the value of $ a is changed, and the value of $ B is also changed.
$ 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.
An interesting example is shown on oschina. the code is as follows:
-
- $ A = array ('Abe ', 'Ben', 'Cam ');
- Foreach ($ a as $ k ==>&$ n)
- $ N = strtoupper ($ n );
- Foreach ($ a as $ k =>$ n) // notice NO reference here!
- Echo "$ nn ";
- Print_r ($ );
- ?>
- Will result in:
-
- ABE
- BEN
- BEN
- Array
- (
- [0] => ABE
- [1] => BEN
- [2] => BEN
- )
- // Explanation: the loop in the second foreach is as follows:
- Array
- (
- [0] => ABE
- [1] => BEN
- [2] => ABE
- )
- Array
- (
- [0] => ABE
- [1] => BEN
- [2] => BEN
- )
- Array
- (
- [0] => ABE
- [1] => BEN
- [2] => BEN
- )
Because there is no unset ($ n), it always points to the last element of the array. the first loop in the second foreach changes $ n, that is, $ a [2] to ABE, the second cycle is changed to BEN, and the third cycle is BEN.