Defined
- Pass value: is to assign the value of the argument to the row parameter, then the modification of the row parameter will not affect the value of the argument
- Pass-through reference: After the real address of the way parameters are passed, both the row and the arguments are the same object, except that their name is different, the modification of the row parameter will affect the value of the argument
Description
- Pass value: The same as copy. For example, I have a house, I give you building materials, you build a house that is the same as mine, you do nothing in your house will affect me, what I do in my house will not affect you, independent of each other.
- Citation: It reminds me of the pointer to the C language when I was in college, and I felt pretty much the same. For example, I have a house, I give you a key, two of us can enter the house, what you do in the House will affect me.
Instance
1, pass Value
- <?php
- $param 1=1; //define variables 1
- $param 2=2; //define Variables 2
- $param 2 = $param 1; //variable 1 assigned to variable 2
- echo $param 2; //Display as 1
- ?>
2, passing references
- <?php
- $param 2=1; //define Variables 2
- $param 1 = &$param 2; //Pass reference to variable 2 to variable 1
- echo $param 2; //Display as 1
- $param 1 = 2; //Assign 2 to the variable 1
- echo $param 2; //Display as 2
- ?>
3, Function pass value
- <?php
- Pass Value
- $param 1 = 1; //define variables 1
- function Add ($param 2) //Pass Parameters
- {
- $param 2=3; //Assign 3 to the variable 2
- }
- $param 3=add ($param 1); //Call method Add and pass the variable 1 to the variable 2
- echo ' <br> $param 1== '. $param 1. ' <br> '; //Display as $param1==1
- echo ' <br> $param 2== '. $param 2. ' <br> '; //Display as $param2== because $PARAM2 is a local variable, it cannot affect the global
- echo ' <br> $param 3== '. $param 3. ' <br> '; //Display as $param3== because the Add method does not return a value, $param3 is empty
- ?>
4, Function Pass Reference
- <?php
- Pass Value
- $param 1 = 1; //define variables 1
- function Add (&$param 2) //Pass Parameters
- {
- $param 2=3; //Assign 3 to the variable 2
- return $param 2; Return variable 2
- }
- echo ' <br> $param 1== '. $param 1. ' <br> '; //Display as $param1==1 no action on variable 1
- $param 3=add ($param 1); //Call method Add and pass a reference to variable 1 to the variable 2
- echo ' <br> $param 1== '. $param 1. ' <br> '; //Show as $param1==3 call variable process, $param 2 change affects variable 1, although no return
- echo ' <br> $param 2== '. $param 2. ' <br> '; //Display as $param2== because $PARAM2 local variables, it cannot affect the global
- echo ' <br> $param 3== '. $param 3. ' <br> '; //Display as $param3== if the return comment inside the method is removed, it is $param3==3
- ?>
5, Function Pass reference 2
- <?php
- Pass Reference
- $param 1 = 1;
- Function &add (&$param 2)
- {
- $param 2 = 2;
- return $param 2;
- }
- $param 3=&add ($param 1);
- $param 4=add ($param 1);
- echo ' <br> $param 3== '. $param 3. ' <br> '; //Display as $param3==2
- echo ' <br> $param 4== '. $param 4. ' <br> '; //Display as $param4==2
- echo ' <br> $param 1== '. $param 1. ' <br> '; //Show as $param1==2 call variable process, $param 2 Change effect variable 1
- $param 3++;
- /* shown below as $param1==3, this is because $param3 and $param1 refer to the same place,
- * The return value is preceded by an address symbol or a reference $param3=&add ($param 1);
- * So $param3, $param 2 and $param1 refer to the same place, when $param3++,
- * $param 1 will be changed */
- echo ' <br> $param 1== '. $param 1. ' <br> ';
- $param 4++;
- /* shown below as $param1==3, why this is 3 instead of 4, because the return value is not preceded by
- * Address symbol, which is not a reference so when $param4 changes, it does not affect $param1*/
- echo ' <br> $param 1== '. $param 1. ' <br> ';
- ?>
A small example that distinguishes PHP reference passing and value passing