Passing by reference
You can pass a variable to a function by reference so that the function can modify the value of its parameter. The syntax is as follows:
<?php function foo(&$var) { $var++; } $a=5; foo($a); // $a is 6 here ?>
Note that there are no reference symbols in the function call - only in the function definition. The function definition alone is enough for parameters to be passed correctly by reference. In recent versions of PHP, if you use & in foo(&$a); you will get a warning that "Call-time pass-by-reference" is deprecated.
The following can be passed by reference:
Variables, such as foo($a)
New statements, such as foo(new foobar())
References returned from functions , for example:
<?php function &bar() { $a = 5; return $a; } foo(bar()); ?>
For detailed explanation, see reference return.
Any other expression cannot be passed by reference, and the result is undefined. For example, the following example of passing by reference is invalid:
<?php function bar() // Note the missing & { $a = 5; return $a; } foo(bar()); // 自 PHP 5.0.5 起导致致命错误 foo($a = 5) // 表达式,不是变量 foo(5) // 导致致命错误 ?>
These conditions are available in PHP 4.0.4 and later versions.
Reference return
Reference return is used when you want to use a function to find which variable the reference should be bound to. Don't use return references to increase performance, the engine is smart enough to optimize it itself. Only return references if there is a valid technical reason! To return a reference, use this syntax:
<?php class foo { public $value = 42; public function &getValue() { return $this->value; } } $obj = new foo; $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42. $obj->value = 2; echo $myValue; // prints the new value of $obj->value, i.e. 2. ?>
In this example, the properties of the object returned by the getValue function will be assigned values, not copied, just as if no reference syntax was used.
Note: Unlike parameter passing, the ampersand must be used in both places here - indicating that a reference is returned, not the usual copy, and also indicating that $myValue is bound as a reference , instead of the usual assignment.
Note: If you try to return a reference from a function like this: return ($this->value);, this will not work because you are trying to return the result of an expression rather than a referenced variable. You can only return reference variables from functions - there is no other way. If code attempts to return the result of a dynamic expression or the new operator, an E_NOTICE error is issued starting with PHP 4.4.0 and PHP 5.1.0.
<?php function &test(){ static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } $a=test();//这条语句会输出$b的值为1 $a=5; $a=test();//这条语句会输出$b的值为2 $a=&test();//这条语句会输出$b的值为3 $a=5; $a=test();//这条语句会输出$b的值为6 ?>
$a=test() method calls a function, which just assigns the value of the function to $a. Any changes made to $a will not affect $b in the function, and through $ When calling a function using a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, which produces the equivalent of this effect ($a =&b;) So changing the value of $a also changes the value of $b, so after executing $a=&test(); $a=5;, the value of $b becomes 5.
PHP passing parameters by reference usage
<?php function add_some_extra(&$string) // 引入变量,使用同一个存储地址 { $string .= 'and something extra.'; } $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?>
Output:
This is a string, and something extra.
If there is no ampersand
<?php function add_some_extra($string) { $string .= 'and something extra.'; } $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, ' ?>
Output:
This is a string,
The above is the detailed content of Detailed explanation of php reference definition and reference passing parameter instance usage. For more information, please follow other related articles on the PHP Chinese website!