PHP 8.3.0 RC 6 available for testing

引用传递

可以将一个变量通过引用传递给函数,这样该函数就可以修改其参数的值。语法如下:

<?php
function foo(&$var)
{
$var++;
}

$a=5;
foo($a);
// 这里 $a 是 6
?>
注意在函数调用时没有引用符号——只有函数定义中有。光是函数定义就足够使参数通过引用来正确传递了。

以下内容可以通过引用传递:

  • 变量,例如 foo($a)
  • 从函数中返回的引用,例如:

    <?php
    function foo(&$var)
    {
    $var++;
    }
    function &
    bar()
    {
    $a = 5;
    return
    $a;
    }
    foo(bar());
    ?>
    详细解释见引用返回

任何其它表达式都不能通过引用传递,结果未定义。例如下面引用传递的例子是无效的:

<?php
function foo(&$var)
{
$var++;
}
function
bar() // 注意缺少 &
{
$a = 5;
return
$a;
}
foo(bar()); // 导致 notice 信息

foo($a = 5) // 表达式,不是变量
foo(5) // 导致致命错误

class Foobar
{
}

foo(new Foobar()) // 自 PHP 7.0.7 起生成通知
// 注意:只有变量应该通过引用传递
?>

add a note

User Contributed Notes 18 notes

up
434
tnestved at yahoo dot com
9 years ago
By removing the ability to include the reference sign on function calls where pass-by-reference is incurred (I.e., function definition uses &), the readability of the code suffers, as one has to look at the function definition to know if the variable being passed is by-ref or not (I.e., potential to be modified). If both function calls and function definitions require the reference sign (I.e., &), readability is improved, and it also lessens the potential of an inadvertent error in the code itself. Going full on fatal error in 5.4.0 now forces everyone to have less readable code. That is, does a function merely use the variable, or potentially modify it...now we have to find the function definition and physically look at it to know, whereas before we would know the intent immediately.
up
30
ccb_bc at hotmail dot com
4 years ago
<?php
// PHP >= 5.6

// Here we use the 'use' operator to create a variable within the scope of the function. Although it may seem that the newly created variable has something to do with '$x' that is outside the function, we are actually creating a '$x' variable within the function that has nothing to do with the '$x' variable outside the function. We are talking about the same names but different content locations in memory.
$x = 10;
(function() use (
$x){
$x = $x*$x;
var_dump($x); // 100
})();
var_dump($x); // 10

// Now the magic happens with using the reference (&). Now we are actually accessing the contents of the '$y' variable that is outside the scope of the function. All the actions that we perform with the variable '$y' within the function will be reflected outside the scope of this same function. Remembering this would be an impure function in the functional paradigm, since we are changing the value of a variable by reference.
$y = 10;
(function() use (&
$y){
$y = $y*$y;
var_dump($y); // 100
})();
var_dump($y); // 100
?>
up
32
mike at eastghost dot com
8 years ago
beware unset() destroys references

$x = 'x';
change( $x );
echo $x; // outputs "x" not "q23" ---- remove the unset() and output is "q23" not "x"

function change( & $x )
{
unset( $x );
$x = 'q23';
return true;
}
up
1
tianyiw at vip dot qq dot com
2 years ago
I designed a class that can easily pass references.

<?php
#### Problem 1
function problem(&$value)
{
}
problem(1); // cannot be passed by reference

#### Problem 2
class problem2
{
static function
__callStatic($name, &$arguments) // cannot take arguments by reference
{
}
}
?>

My solution