Rasmus Lerdorf大师的解答

幸运可以向php创始人直接请教问题 哈哈

$a = 2;

a(&$a);

function a($a){

$b = 1;

$a = &$b;

}

echo $a;

why $a is 2 not 1

10分钟就回信了……

You are treating references like C pointers there.
They aren’t addresses in memory.

When you call your function a local variable called $a
is created which is a reference to the global variable $a.

Then you do:

$b = 1;
$a = &$b;

Now you are moving the reference of $a to be a reference to
the function-local variable $b.  You have now lost the reference
to the global $a.  When the function ends, both $a and $b drop
out of scope.

-Rasmus

Social tagging:

发表评论