Haha, will use only the initial stage, to understand the principle of what, so as to better use, crap not much to say
Pass value: The value of the argument is assigned to the row parameter, the modification of the row parameter does not affect the value of the argument
To pass a reference: True to address the way the argument passed, the row and the arguments are the same object, but their names are different. Changes to a row parameter affect the value of the argument
Description
Pass value: Root copy is the same. For example, I have a brand-new house, I give you building materials, you built a house that is exactly the same as my house, what you do in your house will not affect me, what I do in my house will not affect you, independent of each other.
Quote: It reminds me of the pointers to learning C when I was in college, and I felt almost the same. For example, I have a brand-new house, I give you a key, we all two can enter this House, what you do in the House will affect me.
One, php instance
1, transfer value
Copy Code code as follows:
<?php
$param 1=1; Define variable 1
$param 2=2; Define Variable 2
$param 2 = $param 1; Variable 1 assignment to variable 2
echo $param 2; Displayed as 1
?>
2, passing references
Copy Code code as follows:
<?php
$param 2=1; Define Variable 2
$param 1 = & $param 2; Pass a reference to variable 2 to variable 1
echo $param 2; Displayed as 1
$param 1 = 2; Assign 2 to the variable 1
echo $param 2; Displayed as 2
?>
3, Function Transfer value
Copy Code code as follows:
<?php
Pass Value
$param 1 = 1; Define variable 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 variable 1 to variable 2
Echo ' <br> $param 1== '. $param 1. ' <br> '; Show as $param1==1
Echo ' <br> $param 2== '. $param 2. ' <br> '; Show as $param2== because $PARAM2 is a local variable, you 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
Copy Code code as follows:
<?php
Pass Value
$param 1 = 1; Define variable 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 variable 2
Echo ' <br> $param 1== '. $param 1. ' <br> '; Shown as $param1==3 call variable process, $param 2 change affects variable 1, although no return
Echo ' <br> $param 2== '. $param 2. ' <br> '; Show as $param2== because $PARAM2 local variables, you cannot affect the global
Echo ' <br> $param 3== '. $param 3. ' <br> '; Shown as $param3== If you remove the return comment from the method, it is $param3==3.
?>
5, Function Pass reference 2
Copy Code code as follows:
<?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> '; Show as $param3==2
Echo ' <br> $param 4== '. $param 4. ' <br> '; Show as $param4==2
Echo ' <br> $param 1== '. $param 1. ' <br> '; Shown as $param1==2 call variable process, $param 2 change affects variable 1
$param 3++;
/* The following is shown as $param1==3, because $param2 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++;
/* The following is shown as $param1==3, why here is 3 instead of 4, because the return value is not preceded by
* Address symbol, it is not a reference so when $param4 change does not affect $param1*/
Echo ' <br> $param 1== '. $param 1. ' <br> ';
?>
Haha, but I think the reference will be better, less resources. There is no obvious gap between the tests, perhaps because the test data is not large enough, and if there is more data to test, I think there will be a significant difference.