In php, you only need to add an ampersand ($ aamp; $ B) before the original object; in fact, the reference in php is that two variables with different names point to the same value. Reference is what reference in PHP means accessing with different names... 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:
2. reference and pass the value in the function,The code is as follows:
3. pass the reference value of the object,The code is as follows:
Name; // output real madridecho $ c-> name here; // output real madrid $ B-> name = "ronaldo"; echo $ c-> name here; // output ronaldo here?>
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:
It won't unset $ B, just $ a, for example, reference transfer, test1.php, the code is as follows:
The code for test2.php is as follows:
"; Echo $ B; // output 101/******************************** pay attention to call_user_func_array the following parameter is required: & ****************************** // above "test ($ B ); do not add the & symbol before $ B in ", but in the function" call_user_func_array ", to reference the parameter passing, you need the & symbol, 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:
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:
& $ N) $ n = strtoupper ($ n); foreach ($ a as $ k => $ n) // notice NO reference here! Echo "$ nn"; print_r ($ a);?> Will result in: abe 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.
Address:
Reprinted at will, but please attach the article address :-)