background image

    foo ($a);

    // $a is 6 here

    ?>

 

注意在函数调用时没有引用符号 - 只有函数定义中有.光是函数定义就足够使参数通

过引用来正确传递了.

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

 

例如 foo($a)

New 语句,

 

例如 foo(new foobar())

从函数中返回的引用,例如:
<?php

    function &bar() {

    $a = 5;

    return $a;

    }

    foo(bar());

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

    function bar(){ // Note the missing &

    $a = 5;

    return $a;

    }

    foo(bar());

    foo($a = 5) // 表达式,不是变量

    foo(5) // 常量,不是变量

    ?>