Haha, knowing how to use it is only at the initial stage. You need to understand what the principle is so that you can use it better. Without further ado
Passing value: assigning the value of the actual parameter to the row parameter, then the modification of the row parameter does not Will affect the value of the actual parameter
Passing by reference: After the parameter is truly passed by address, the line parameter and the actual parameter are the same object, but their names are different. Modification of the line parameter will affect the value of the actual parameter.
Description:
Passed value: The root copy is the same. For example, I have a house and I give you building materials. You build a house that is exactly the same as mine. Whatever you do in your house will not affect me. What I do in my house will not affect me. Nothing will affect you, they are independent of each other.
Passing by reference: It reminds me of the pointers I learned in C language when I was in college. It feels almost the same. For example, I have a house. If I give you a key, both of us can enter the house. Whatever you do in the house will affect me.
1, php instance
1, passing value
Copy code The code is as follows:
$param1=1; //Define variable 1
$param2=2; //Define variable 2
$param2 = $param1; //Assign variable 1 to variable 2
echo $param2; //displayed as 1
?>
2, pass reference
Copy code The code is as follows:
$param2=1; //Define variable 2
$param1 = &$param2; //Pass the reference of variable 2 to variable 1
echo $param2; //Displayed as 1
$param1 = 2; //Assign 2 to variable 1
echo $param2; //Displayed as 2
?>
3, function transfer value
Copy code The code is as follows:
/ /Pass value
$param1 = 1; //Define variable 1
function add($param2) //Pass parameter
{
$param2=3; //Assign 3 to variable 2
}
$param3=add($param1); //Call the method add and pass variable 1 to variable 2
echo '
$param1=='.$param1.'< br>'; //Displayed as $param1==1
echo '
$param2=='.$param2.'
'; //Displayed as $param2== because $param2 is Local variables, so they cannot affect the global
echo '
$param3=='.$param3.'
'; //Displayed as $param3== Because the add method has no return value, $param3 Is empty
?>
4, function passing reference
Copy code The code is as follows:
//Pass value
$param1 = 1; //Define variable 1
function add(&$param2) //Pass parameter
{
$ param2=3; //Assign 3 to variable 2
// return $param2; //Return variable 2
}
echo '
$param1=='.$param1.'< ;br>'; //Displayed as $param1==1, variable 1 is not operated
$param3=add($param1); //Call the method add and pass the reference of variable 1 to variable 2
echo '
$param1=='.$param1.'
'; //Displayed as $param1==3 During the process of calling variables, the change of $param2 affects variable 1, although there is no return
echo '
$param2=='.$param2.'
'; //Displayed as $param2== Because $param2 is a local variable, it cannot affect the global world
echo '
$param3=='.$param3.'
'; //Displayed as $param3== If you remove the return comment in the method, it will be $param3==3
?>
5, function reference 2
Copy code The code is as follows:
//Pass reference
$param1 = 1;
function &add(&$param2)
{
$param2 = 2;
return $param2;
}
$param3=&add($param1);
$param4=add($param1);
echo '
$param3=='.$param3. '
'; //Displayed as $param3==2
echo '
$param4=='.$param4.'
'; //Displayed as $param4==2
echo '
$param1=='.$param1.'
'; //Displayed as $param1==2 During the process of calling variables, the change of $param2 affects variable 1
$ param3++;
/*The following shows $param1==3. This is because $param2 and $param1 refer to the same place.
* Is the return value preceded by an address symbol or a reference? $param3=&add($ param1);
* In this way, $param3, $param2 and $param1 refer to the same place. When $param3++;,
* $param1 will be changed*/
echo '
$param1 =='.$param1.'
';
$param4++;
/* The following shows $param1==3. Why is it 3 instead of 4? This is because there is no
* Address symbol, it is not a reference so it will not affect $param1 when $param4 changes*/
echo '
$param1=='.$param1.'
';
?>
Haha, but I think passing a reference would be better and consume less resources. There is no obvious gap in the above test, which may be because the test data is not large enough. If there is more data to test, I think there will be a significant difference.
http://www.bkjia.com/PHPjc/325762.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325762.htmlTechArticleHaha, knowing how to use it is just the initial stage. You need to understand what the principle is so that you can use it better. Not much to say. Pass-by-value: The value of the actual parameter is assigned to the row parameter, so the modification of the row parameter does not...