Five examples of php illustrate the difference between a pass value and a pass reference

  • 2020-05-19 04:19:51
  • OfStack

Ha ha, can use is only primary stage, want to understand the principle is what, so that can better go to use, waste words not to say
Pass value: the value of the argument is assigned to the argument, so the change of the argument does not affect the argument value
Passing a reference: after the actual parameter is passed in an address, the line arguments and arguments are the same object, but their names are different. Changes to the line arguments will affect the value of the argument
Description:
Pass value: the root copy is 1-like. For example, if I have a house, I give you building materials, you build a house that is one model of my house, nothing you do in your house will affect me, nothing I do in my house will affect you, they are independent of each other.
It reminds me of learning the C language in college. 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.
1, php instance
1, the value
 
<?php 
$param1=1; // Define variables 1 
$param2=2; // Define variables 2 
$param2 = $param1; // variable 1 Assign a value to a variable 2 
echo $param2; // Is shown as 1 
?> 

2. Quote
 
<?php 
$param2=1; // Define variables 2 
$param1 = &$param2; // The variable 2 Is passed to the variable 1 
echo $param2; // Is shown as 1 
$param1 = 2; // the 2 Assign a value to a variable 1 
echo $param2; // Is shown as 2 
?> 

3. Function passing
 
<?php 
// The value of  
$param1 = 1; // Define variables 1 
function add($param2) // The parameter  
{ 
$param2=3; // the 3 Assign a value to a variable 2 
} 
$param3=add($param1); // A method is called add , and will variable 1 To the variable 2 
echo '<br>$param1=='.$param1.'<br>'; // Is shown as $param1==1 
echo '<br>$param2=='.$param2.'<br>'; // Is shown as $param2==  because $param2 It is a local variable, so it cannot affect the whole  
echo '<br>$param3=='.$param3.'<br>'; // Is shown as $param3==  because add Method does not return a value, so $param3 Is empty  
?> 

4. Function pass reference
 
<?php 
// The value of  
$param1 = 1; // Define variables 1 
function add(&$param2) // The parameter  
{ 
$param2=3; // the 3 Assign a value to a variable 2 
// return $param2; // Returns the variable 2 
} 
echo '<br>$param1=='.$param1.'<br>'; // Is shown as $param1==1  Not for variable 1 operate  
$param3=add($param1); // A method is called add , and will variable 1 Is passed to the variable 2 
echo '<br>$param1=='.$param1.'<br>'; // Is shown as $param1==3  In calling a variable, $param2 Change the influence variables of 1 Although not return 
echo '<br>$param2=='.$param2.'<br>'; // Is shown as $param2==  because $param2 Local variables, so they don't affect the whole  
echo '<br>$param3=='.$param3.'<br>'; // Is shown as $param3==  If you take the inside of the method return If you take it out of the comment, it's zero $param3==3 
?> 

5, function transfer reference 2
 
<?php 
// The reference  
$param1 = 1; 
function &add(&$param2) 
{ 
$param2 = 2; 
return $param2; 
} 
$param3=&add($param1); 
$param4=add($param1); 
echo '<br>$param3=='.$param3.'<br>'; // Is shown as $param3==2 
echo '<br>$param4=='.$param4.'<br>'; // Is shown as $param4==2 
echo '<br>$param1=='.$param1.'<br>'; // Is shown as $param1==2  In calling a variable, $param2 Change the influence variables of 1 
$param3++; 
/* The following is shown as $param1==3 That's because $param2 and $param1 With reference to the 1 A place,  
*  The return value is preceded by an address symbol or 1 A reference $param3=&add($param1); 
*  such $param3,$param2 and $param1 With reference to the 1 A place, when $param3++; When,  
* $param1 Will be changed */ 
echo '<br>$param1=='.$param1.'<br>'; 
$param4++; 
/*  The following is shown as $param1==3, Why is this 3 Rather than 4 Well, that's because the return value is not in front of it  
*  Address symbol, it's not 1 So when $param4 It doesn't matter when you change it $param1*/ 
echo '<br>$param1=='.$param1.'<br>'; 
?> 

Haha, but I think it would be 1 point better to pass the quote and consume less resources. There is no obvious gap in the above test, which may be caused by the insufficient test data. If there is more data to test, I think there will be a significant difference.

Related articles: