PHP pass by reference / value - question

I was going to explain to our intern the difference between "pass by reference" and "pass by value" in PHP, and made this simple script:

$a=5; $b=&$a; $a=8; echo $b; // prints 8 $a=5; $b=$a; //no & $a=8; echo $b; // prints 5 

However, doing this in php-cli using php -qa gives:

 php > $a=5; php > $b=&$a; php > $a=8; php > echo $b; 8 php > // prints 8 php > $a=5; php > $b=$a; //no & php > $a=8; php > echo $b; 8 php > // prints 5 

Should $b=$a; disable $ a and $ b?

... so I got curius and tried:

 php > $b=3; php > echo $a; 3 

So how do I get it wrong? What's going on here? It seems that the reference setting is somehow sticking, although it should be cleared on the line $b=$a ? I also tried:

 php > $e=5; $f=$e; $e=6; echo $f; 5 

... works as expected.

$a and $b seem to be connected all the time? Did I miss any big point here? How to "unlink" with the variable $a and $b ?

+4
source share
6 answers

Why should a link be cleared if you assign a value to a variable? It works like this (with semi-simplified comments):

 $a = 5; // creates a "slot", puts 5 in it, makes $a point to it $b =& $a; // makes $b point to the same "slot" $a points to $c = 6; // creates a "slot", puts 6 in it, makes $c point to it $a = $c; // puts the value of the slot $c points to into the slot $a points to echo $b; // outputs the value of the slot $b points to (6) 

It assigns a value to a variable. Whether the value is alphabetic ( 5 ) or a value belonging to another variable does not matter. The link remains until you unset($b) .

+6
source

OK, yes, because you created $b as a link to $a . So what do you do on the line:

 $b = $a 

just assigns 5 to $ a, because $b still refers to $a .

If you want "unreference", you will need to unset and recreate the variable.

+1
source

The first time your code uses $ b, it creates the variable $ b and associates it with the address $ a. Subsequently, you write the values ​​to the shared folder. You cannot undo where the variable refers.

0
source

Following my comment ... I ran your script - and in both cases it printed "8". I added unset($b) after the first echo and started again - now the result was 85, as expected.

0
source

The problem is that when you do $b = $a , since $ b is a reference to $ a, you actually do $a = $a . As other people said, you need unset($b) .

0
source

Perhaps using more speaker variable names will make this clearer for you:

 $value = 5; $alias = &$value; $value = 8; echo $alias; # 8 $value = 5; $alias = $value; # no & $value = 8; echo $alias; # 8 (OP expected 5) 

Easier to read, right?

Pay attention, especially to this line:

 $alias = $value; # no & 

What's going on here?

Since $alias is an alias of $value , you basically write:

 $value = $value; 

At this stage it is 5.

Then you set $value to 8.

$alias still refers to $value .

If you want to stop $alias as an alias of $value , you can turn it into an alias for something else:

 $alias = &$other; 

Or just disable the link:

 unset($alias); 
0
source

All Articles