Passing by reference, $ a = & $ b, is $ a = $ n the same as $ b = $ n?

I'm trying to figure out a link. This is probably a bad analogy, but does it look like Newtonโ€™s third law (action-reaction pairs)?

For example, for the following code

$a = 4; $b = 2; $n = 42; $a = &$b; 

is an

Is $a=$n the same as $b=$n ? Is $a and $b stored at the same address?

+4
source share
4 answers

If you usually assign these variables:

 $a = 1; $b = 2; $c = 3; 

Then the associations will look like this:

 a --> 1 b --> 2 c --> 3 

Now, if you enter $c in the link $a , for example:

 $a = 1; $b = 2; $c = &$a; 

Then the associations will look like this:

 a --> 1 <--. b --> 2 | c --------/ 

In other words, $a and $c point to the same value. Since they both point to the same value, we can change either of the variables, and both of them point to a new value.

 $a = 5; echo "$a $c"; // Output: "5 5" $c = 10; echo "$a $c"; // Output: "10 10" 
+5
source

Yes After assigning $n - $a , $b will indicate $n .

This is because after the execution of $a=&$b both $a and $b refer to the same memory cell and become a reference variable ( is_ref=1 ) . And the reference counter ( refcount ) of this particular memory location is incremented by 1 . Now, any value that you assign to any of these links will point to the same value.

Doing $a=$n means that the value of $n will be stored in the location referenced by $a . And this is the same place as $b .

See an example here.

$a , $b , $n indicate different locations

 php > $a = 4; php > $b = 2; php > xdebug_debug_zval('a'); // they are pointing different location a: (refcount=1, is_ref=0)=int(4) php > xdebug_debug_zval('b'); // they are pointing different location b: (refcount=1, is_ref=0)=int(2) php > $n = 42; php > xdebug_debug_zval('n'); n: (refcount=1, is_ref=0)=int(42) 

$ a and $b both now become a link

 php > $a = &$b; php > xdebug_debug_zval('b'); b: (refcount=2, is_ref=1)=int(2) php > xdebug_debug_zval('a'); // a too a: (refcount=2, is_ref=1)=int(2) 

Assigning a new value , NOT references to any of $a and $b

 php > $a = $n; php > xdebug_debug_zval('a'); // a holds $n value '42' now a: (refcount=2, is_ref=1)=int(42) php > xdebug_debug_zval('b'); // same for b b: (refcount=2, is_ref=1)=int(42) 
+1
source

Usually, when you create the variable $a and $b , each has a unique memory address where their data is stored.

However, if you tell the interpreter that $a = &$b , this means that $ a and $ b now have the same memory address. If you assign something to $ a or $ b, both of them will be an echo of the same value, because they store data in the same place in memory.

If you want to experiment, I suggest starting the interactive PHP interpreter from the command line:

 php -a php > $a = 1; php > $b = 2; php > echo $a . ' ' . $b; 1 2 php > $a = &$b; php > echo $a . ' ' . $b; 2 2 php > $a = 1; php > echo $a . ' ' . $b; 1 1 php > $b = 2; php > echo $a . ' ' . $b; 2 2 
0
source

$a = $b and $b = $n have the same value, but differ from each other because the value is a primitive type and primitive types pass by value .

The fastest way to check if the code of the link is using or not, changes the value of the source var and sees if its target vars changed its value or not.

 $n = 1; $a = $n; $b = $n; echo $a; // 1 echo $b; // 1 echo $n; // 1 $n = 2; echo $a; // 1 echo $b; // 1 echo $n; // 2 

However objects always are passed by reference

 $n = new Object(1); $a = $n; $b = $n; $n->newValue(5); $a->printValue(); // 5 $b->printValue(); // 5 $a->newValue(7); $b->printValue(); // 7 
-1
source

All Articles