Difference: Passing value means copying the content of the original variable and saving it in a new memory space. The two variables are independent of each other. Modifying one variable will not affect the other variable. Pass-by-reference (pass-by-reference) is equivalent to giving the current variable an alias. In fact, the two variables refer to the same value, and modifying one variable will affect the other variable.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Value transfer
Value passing is the default value passing method for functions in PHP, also known as "copy passing by value". As the name suggests, the value passing method will copy the value of the actual parameter and then pass it to the formal parameter of the function, so operating the value of the parameter in the function will not affect the actual parameters outside the function. Therefore, if you do not want the function to modify the value of the actual parameter, you can pass it by value.
[Example] The following defines a simple function. The function has two parameters, and the values of the parameters are exchanged in the function.
<?php function swap($a, $b){ echo '函数内,交换前 $a = '.$a.', $b = '.$b.'<br>'; $temp = $a; $a = $b; $b = $temp; echo '函数内,交换后 $a = '.$a.', $b = '.$b.'<br>'; } $x = 5; $y = 7; echo '函数外,交换前 $x = '.$x.', $y = '.$y.'<br>'; swap($x, $y); echo '函数外,交换后 $x = '.$x.', $y = '.$y; ?>
The running results are as follows:
函数外,交换前 $x = 5, $y = 7 函数内,交换前 $a = 5, $b = 7 函数内,交换后 $a = 7, $b = 5 函数外,交换后 $x = 5, $y = 7
It can be seen from the running results that the values are indeed exchanged within the function, but outside the function, the values do not change. So we can say that passing a function by value is just passing a copy of the variable. So if you want the function to be able to operate on external parameters of the function, you need to use reference passing.
Address passing (reference passing)
Parameter passing by reference is to copy the memory address of the actual parameter and then pass it to the formal parameter, actual parameter and The formal parameters all point to the same memory address, so the function's operation on the formal parameters will affect the actual parameters outside the function.
Passing by reference is to pass the memory address of the actual parameter to the formal parameter of the function. Therefore, the actual parameters and formal parameters point to the same memory address. At this time, all operations inside the function will affect the values of the actual parameters outside the function. The method of passing by reference is to add an & symbol on the basis of value passing, as shown below:
function name (&参数1, &参数2, ..., &参数3) { ... }
[Example] Slightly adjust the code of the above example and use the method of passing by reference to pass to the swap function. Parameters and code are as follows:
<?php function swap(&$a, &$b){ echo '函数内,交换前 $a = '.$a.', $b = '.$b.'<br>'; $temp = $a; $a = $b; $b = $temp; echo '函数内,交换后 $a = '.$a.', $b = '.$b.'<br>'; } $x = 5; $y = 7; echo '函数外,交换前 $x = '.$x.', $y = '.$y.'<br>'; swap($x, $y); echo '函数外,交换后 $x = '.$x.', $y = '.$y; ?>
The running results are as follows:
函数外,交换前 $x = 5, $y = 7 函数内,交换前 $a = 5, $b = 7 函数内,交换后 $a = 7, $b = 5 函数外,交换后 $x = 7, $y = 5
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the difference between passing value and passing address in php. For more information, please follow other related articles on the PHP Chinese website!