Home >Backend Development >PHP Tutorial >Detailed explanation of passing by value and passing by address (reference) in PHP functions_PHP Tutorial
In PHP, it is relatively simple for us to pass values by function, but some naive friends may not understand function passing by address or reference. The following editor will introduce to you the function passing by value and address (reference) in PHP. ) introduction, I hope it will be helpful to you.
Usage quoted in php:
1. Reference assignment of variables: $a = &$b
2. Passing reference parameters when calling functions
1) In early PHP, reference type variables were passed through the & symbol when calling, such as: func(&$arg);
2) Later, the reference type parameters of the function were stipulated to need to be defined when the function was declared, instead of: function func(&$arg);
Note: After defining reference type parameters during reference declaration, runtime reference parameter passing is abandoned. You need to add allow_call_time_pass_reference to php.ini to enable it.
3. The function returns a reference type. This application method requires adding an & symbol before the function name when declaring the function, and when calling, use the reference assignment method, such as:
The code is as follows | Copy code | ||||
function &func() { Return $a; } $a = func(); //This method of calling does not result in pass-by-reference $a =& func(); //This is a call by reference
|
代码如下 | 复制代码 |
$a = 1; |
The code is as follows | Copy code |
$a = 1; function &func(&$a) { return $a; } $b = func($a); $c =& func($a); $b = 2; echo "a: $a, b: $b, c: $c. /n"; //Output a: 1, b: 2, c: 1. //It can be seen that the modification of $b will not affect $a $c = 3; echo "a: $a, b: $b, c: $c. /n"; //Output a: 3, b: 2, c: 3. //It can be seen that the modification of $c will affect $a |
Several details of value transfer in php function
The code is as follows | Copy code | ||||
//1. Value passing of basic data types
|
What is the difference between passing by value and passing by address in PHP function? I like to get answers to questions like this through program demonstrations. Let’s take a look at a demo recording!
代码如下 | 复制代码 |
$i=100; function func($n){ $n=$n+100; return $n; } echo '1)函数传值前变量$i的值:'.$i.' '; echo '2)传值后函数的返回值:'.func($i).' '; echo '3)函数传值后变量$i的值:'.$i.' '; echo '4)函数传址前变量$i的值:'.$i.' '; echo '5)传址后函数的返回值:'.func(&$i).' '; echo '6)函数传址后变量$i的值:'.$i.' '; //echo func($i).' '; ?> |
Program output:
1) The value of variable $i before the function passes the value: 100
2) The return value of the function after passing the value: 200
3) The value of variable $i after the function passes the value: 100
4) The value of variable $i before function transfer: 100
5) The return value of the function after passing the address: 200
6) The value of variable $i after function transfer: 200
Explanation:
1) Directly output a variable assigned $i=100
2) The func function passes a value and returns an arithmetic addition result $=100+100
3) When the func function passes a value, its scope of action is limited to the inside of the function and will not affect the external variable $i
4) Same as above
5) There is an additional "&" character before the func function parameter, indicating address transfer. Like 2), the return value is the arithmetic operation result of the function.
6) func(&$i), the variable pointer points to the position of $i, which can be understood as an operation on the variable $i, $i=$i+100; at this time, the variable $i has been equivalent to being reassigned
What will be the result if you remove the comment on line 18 of the above code?
Summary: Passing by address is to change the function parameter value while executing the function, but passing by value is not expected to change.