Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to understand passing by reference. This is probably a bad analogy, but is it like Newton's 3rd law (action-reaction pairs)?

For example, for the following code

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

is

$a=$n the same as $b=$n? Isn't the value of $a and $b stored in the same address?

like image 517
user784637 Avatar asked Jan 15 '13 07:01

user784637


People also ask

What is passing by reference vs passing by value?

“Passing by value” refers to passing a copy of the value. “Passing by reference” refers to passing the real reference of the variable in memory.

What happens if a variable is passed by reference?

Defining Pass by Reference Pass means to provide an argument to a function. By reference means that the argument you're passing to the function is a reference to a variable that already exists in memory rather than an independent copy of that variable.

What is pass by value and pass by reference in C++?

Definition. Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.

Why do we use pass by reference in C++?

If you recall, using pass by reference allows us to effectively “pass” the reference of a variable in the calling function to whatever is in the function being called. The called function gets the ability to modify the value of the argument by passing in its reference.


2 Answers

If you assign these variables normally:

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

Then the associations will look like this:

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

Now, if you make $c into a reference of $a, like this:

$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. Because they both point to the same value we can change either of the variables and they will both point to the new value.

$a = 5;
echo "$a $c"; // Output: "5 5"
$c = 10;
echo "$a $c"; // Output: "10 10"
like image 110
Sverri M. Olsen Avatar answered Oct 25 '22 10:10

Sverri M. Olsen


Yes, After assigning $n to $a, $b will point $n.

This is because after executing $a=&$b both $a and $b references a same memory location and become reference variable (is_ref=1). And the reference count (refcount) of that specific memory location increases by 1. Now whatever value you assign to any of those references both will point to same value.

Executing $a=$n means value of $n will be store to the location referenced by $a. And this is same location as $b.

See the example here.

$a, $b, $n are pointing 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 becomes a reference now

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 new value, NOT references to any of $a and $b

php > $a = $n;
php > xdebug_debug_zval('a'); // a holds $n's 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)
like image 33
Shiplu Mokaddim Avatar answered Oct 25 '22 10:10

Shiplu Mokaddim