php引用传递

Passing by Reference

   You can pass a variable by reference to a function so the function   can modify the variable. The syntax is as follows:   

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

$a=5;
foo($a);
// $a is 6 here
?>

Note:            There is no reference sign on a function call - only on      function definitions. Function definitions alone are enough to      correctly pass the argument by reference.  As of PHP 5.3.0,      you will get a warning saying that "call-time pass-by-reference" is      deprecated when you use & in foo(&$a);.       And as of PHP 5.4.0, call-time pass-by-reference was removed, so      using it will raise a fatal error.    

  The following things can be passed by reference:  

  •       Variables, i.e. foo($a)
  •       New statements, i.e. foo(new foobar())
  •       References returned from functions, i.e.:   

    <?php
    function foo(&$var)
    {
       
    $var++;
    }
    function &
    bar()
    {
       
    $a = 5;
        return
    $a;
    }
    foo(bar());
    ?>
        See more about returning by reference.     

  No other expressions should be passed by reference, as the  result is undefined. For example, the following examples of passing  by reference are invalid:   

<?php
function foo(&$var)
{
   
$var++;
}
function
bar() // Note the missing &
{
   
$a = 5;
    return
$a;
}
foo(bar()); // Produces fatal error since PHP 5.0.5

foo($a = 5); // Expression, not variable
foo(5); // Produces fatal error
?>
    These requirements are for PHP 4.0.4 and later.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值