Home >Backend Development >PHP Tutorial >Detailed explanation of passing by value and passing by address (reference) in PHP functions_PHP Tutorial

Detailed explanation of passing by value and passing by address (reference) in PHP functions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:37903browse

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();  //这种调用方式得到的不是引用传值
$a =& func(); //这样调用才是引用传值


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

In short, it is to let the function return a reference type value. A more practical example:
 代码如下 复制代码

$a = 1;
function &func(&$a) {
  return $a;
}
$b = func($a);
$c =& func($a);
$b = 2;
echo "a: $a, b: $b, c: $c.
/n";
//输出a: 1, b: 2, c: 1.
//可见对$b的修改不会影响$a
$c = 3;
echo "a: $a, b: $b, c: $c.
/n";
//输出a: 3, b: 2, c: 3.
//可见对$c的修改会影响$a

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
 代码如下 复制代码
//一、基本数据类型的传值  
/* **************************************************** */ 
function testvar($k){
 $k = 40;
}
$c = 30;
//给一个函数参数传一个基本数据类型(整型,布尔,字符 ...), 实际上传的就是值 ;
testvar($c);
echo $c;//结果是:30
 
 
function testvar2(&$k){
 $k = 40;
}
$e = 30;
//给一个函数参数传一个基本数据类型(整型,布尔,字符 ...), 实际上传的y就是地址 ;
testvar2($e);
echo $e;//结果是:40
 
/* **************************************************** */
//二、数组(默认情况下是复制一份数据),如要传地址则&$arr.
$arr1 = array(-1,5,0);
function testArr($arr){
 for($i=0;$i for($j=$i+1;$j if($arr[$i]>$arr[$j]){
    $temp = $arr[$i];
    $arr[$i] = $arr[$j];
    $arr[$j] = $temp;
   }
  }
 
 }
 print_r($arr);  //结果:Array ( [0] => -1 [1] => 0 [2] => 5 )
}
testArr($arr1);
print_r($arr1);  //结果:Array ( [0] => -1 [1] => 5 [2] => 0 )
 
function testArr2(&$arr){
 for($i=0;$i
for($j=$i+1;$j if($arr[$i]>$arr[$j]){
    $temp = $arr[$i];
    $arr[$i] = $arr[$j];
    $arr[$j] = $temp;
   }
  }
 
 }
}
testArr($arr1);
print_r($arr1);  //结果:Array ( [0] => -1 [1] => 0 [2] => 5 )
 
 
 
 
/* **************************************************** */
//三、对象数据类型传值
class person{
 public $name;
 public  $age;
}
 
$a = new person();
$a->name = '小明';
$a->age = '20';
//变量a在存的是对象的地址,把a赋给b这个变量,实际上就是赋了一个地址。
$b = $a;
$b->age = 30;
//echo $a->age.$b->age;//结果是:30 30
//给一个函数参数传一个对象, 实际上传的是这个对象的地址;
function test($k){
 $k->age =40;
}
//调用
test($b);
//echo $a->age.$b->age;//结果是:40 40

/************************************************* **** */ function testvar($k){ $k = 40; } $c = 30; //Pass a basic data type (integer, Boolean, character...) to a function parameter, and what is actually passed is the value; testvar($c); echo $c;//The result is: 30 function testvar2(&$k){ $k = 40; } $e = 30; //Pass a basic data type (integer, Boolean, character...) to a function parameter. In fact, the y passed is the address; testvar2($e); echo $e;//The result is: 40 /************************************************* **** */ //2. Array (by default, a copy of the data is copied). If you want to pass the address, then &$arr. $arr1 = array(-1,5,0); function testArr($arr){ for($i=0;$i for($j=$i+1;$j if($arr[$i]>$arr[$j]){ $temp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $temp; } } } print_r($arr); //Result:Array ( [0] => -1 [1] => 0 [2] => 5 ) } testArr($arr1); print_r($arr1); //Result:Array ( [0] => -1 [1] => 5 [2] => 0 ) function testArr2(&$arr){ for($i=0;$i for($j=$i+1;$j if($arr[$i]>$arr[$j]){ $temp = $arr[$i]; $arr[$i] = $arr[$j]; $arr[$j] = $temp; } } } } testArr($arr1); print_r($arr1); //Result:Array ( [0] => -1 [1] => 0 [2] => 5 ) /************************************************* **** */ //3. Pass value of object data type class person{ public $name; public $age; } $a = new person(); $a->name = 'Xiao Ming'; $a->age = '20'; //The variable a stores is the address of the object. Assigning a to the variable b actually assigns an address. $b = $a; $b->age = 30; //echo $a->age.$b->age;//The result is: 30 30 //Pass an object to a function parameter. What is actually passed is the address of the object; function test($k){ $k->age =40; } //Call test($b); //echo $a->age.$b->age;//The result is: 40 40

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632691.htmlTechArticleIn php, it is relatively simple for us to pass values ​​by function, but some friends may naively pass functions by address or I don’t understand the quotation. Let me introduce to you the function passing by value and address in PHP (citation...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn