Haha, it will only be used in the preliminary stage. We need to understand the principle so that we can use it better.
Pass value: if the value of the real parameter is assigned to the row parameter, the modification of the row parameter will not affect the value of the real parameter.
Pass reference: After passing parameters in real address mode, the row parameters and real parameters are the same object, but their names are different. Modifying the row parameters will affect the values of the real parameters.
Note:
Pass value: the root copy is the same. For example, if I have a house, I will give you building materials, and you have built a house with the same root of my house, what you do in your house will not affect me, what I do in my house will not affect you and be independent of each other.
Reference: reminds me of the pointer to C language when I went to college. It feels similar. For example, if I have a house, and I give you a key, both of us can enter the house. What you do in the house will affect me.
I. php instance
1. Pass the value
Copy codeThe Code is as follows:
<? Php
$ Param1 = 1; // define variable 1
$ Param2 = 2; // defines variable 2
$ Param2 = $ param1; // variable 1 is assigned to variable 2.
Echo $ param2; // 1
?>
2. Upload reference
Copy codeThe Code is as follows:
<? Php
$ Param2 = 1; // defines variable 2
$ Param1 = & $ param2; // pass the reference of variable 2 to variable 1
Echo $ param2; // 1
$ Param1 = 2; // assign 2 to variable 1
Echo $ param2; // 2
?>
3. Pass the function value
Copy codeThe Code is as follows:
<? Php
// Pass the value
$ Param1 = 1; // define variable 1
Function add ($ param2) // PASS Parameters
{
$ Param2 = 3; // assign 3 to variable 2
}
$ Param3 = add ($ param1); // call the add method and pass variable 1 to variable 2.
Echo '<br> $ param1 ='. $ param1. '<br>'; // The value is $ param1 = 1.
Echo '<br> $ param2 ='. $ param2. '<br>'; // displayed as $ param2 = because $ param2 is a local variable, it cannot affect the global
Echo '<br> $ param3 ='. $ param3. '<br>'; // displayed as $ param3 = because the add method does not return a value, $ param3 is empty.
?>
4. Function transfer reference
Copy codeThe Code is as follows:
<? Php
// Pass the value
$ Param1 = 1; // define variable 1
Function add (& $ param2) // PASS Parameters
{
$ Param2 = 3; // assign 3 to variable 2
// Return $ param2; // return variable 2
}
Echo '<br> $ param1 ='. $ param1. '<br>'; // The variable 1 is not operated on $ param1 = 1.
$ Param3 = add ($ param1); // call the add method and pass the reference of variable 1 to variable 2.
Echo '<br> $ param1 = '. $ param1. '<br>'; // when $ param1 = 3 is called, $ param2 changes affect variable 1, although no return
Echo '<br> $ param2 ='. $ param2. '<br>'; // It is displayed as $ param2 = because $ param2 is a local variable, the global condition cannot be affected.
Echo '<br> $ param3 = '. $ param3. '<br>'; // displayed as $ param3 = If you remove the return comment in the method, it is $ param3 = 3.
?>
5. Function transfer Reference 2
Copy codeThe Code is as follows:
<? Php
// Upload reference
$ Param1 = 1;
Function & add (& $ param2)
{
$ Param2 = 2;
Return $ param2;
}
$ Param3 = & add ($ param1 );
$ Param4 = add ($ param1 );
Echo '<br> $ param3 ='. $ param3. '<br>'; // The value is $ param3 = 2.
Echo '<br> $ param4 ='. $ param4. '<br>'; // displayed as $ param4 = 2
Echo '<br> $ param1 ='. $ param1. '<br>'; // It is displayed as $ param1 = 2. Changes in $ param2 affect variable 1.
$ Param3 ++;
/* $ Param1 = 3 is shown below, because $ param2 and $ param1 reference to the same place,
* The return value is preceded by an address symbol or a reference $ param3 = & add ($ param1 );
* In this way, $ param3, $ param2, and $ param1 are referenced in the same place. When $ param3 ++,
* $ Param1 will be changed */
Echo '<br> $ param1 ='. $ param1. '<br> ';
$ Param4 ++;
/* The following shows $ param1 = 3. Why is it 3 instead of 4? This is because there is no
* The address symbol, which is not a reference. Therefore, $ param1 */is not affected when $ param4 is changed */
Echo '<br> $ param1 ='. $ param1. '<br> ';
?>
Haha, but I think it would be better to pass the reference and consume less resources. There is no obvious gap in the above test, probably because the test data is not big enough. If there is more data to test, I think there will be a significant difference.