5

I am trying to figure out the difference between $a=&$b and $a=$b. I know & make the variable to be a reference variable. But the following test gave me the same result. Can anyone explain the difference? Thanks.

 $a=5; $b=6; $a=&$b; echo $a; //6 $a=5; $b=6; $a=$b; echo $a; //6 
2
  • 1
    I cant vote you guys yet..but first one answer got my accepted. Thanks a lot. Commented Jul 24, 2010 at 17:23
  • 1
    possible duplicate of what do "=&" / "&=" operators in php mean? Commented Apr 1, 2012 at 6:17

6 Answers 6

14

First of all: you'll hardly ever need references, avoid the confusion of using them if you can.

$a=5; //assign value to a $b=&$a; //make $b a reference to $a $b=6; //assigning a value to $b assigns the same value to $a (as they point to the same location echo $a; //6 $a=5; //assign a value to a $b=$a; //set $b to the value of $a $b=6; //set $b to another value leaves $a at it's original value echo $a; //5 
Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to say thanks again for your answer on PHP Simple HTML Dom Parser, knowing that you can store a function in a variable will be a massive help in my future projects.
1

It matters more in a function when you send it in as a parameter.

For example:

<?php function changeVariableWithReference(&$var) { $var += 1; } function changeVariableWithoutReference($var) { $var += 1; } $a = 5; $b = 5; changeVariableWithReference($a); changeVariableWithoutReference($b); print $a . ' ' . $b; ?> 

Comments

1

The difference between $a = $b and $a =& $b is that with the former assignment operator the value is copied while with the latter reference operator the variable $a refers to the same value as $b.

You don’t see any difference when reading the value but you see it when writing the value:

$var = 123; $copyOfVar = $var; $referenceToVar =& $var; $copyOfVar = 456; echo 'var='.$var.'; copyOfVar='.$copyOfVar; // "var=123; copyOfVar=456" $referenceToVar = 456; echo 'var='.$var.'; referenceToVar='.$referenceToVar; // "var=456; referenceToVar=456" 

Comments

1

When you use the & in an assignment, you can think of the new variable being a 'short-cut' to the original. If you don't use the &, it will be a 'copy' of the original.

$a = 5; $b =& $a; // this $b is like a shortcut to $a. Change $b and $a will change too $a = 5; $b = $a; // this time, $b will be 5 - but not a shortcut to $a. Change $b and $a will still be 5. 

Comments

1

A reference can be easily explained by simple graph. When you use copy by value ($a = $b) then something like that happens:

$a = 1000; # $a -----> 1000 $b = $a; # $a -----> 1000 # $b -----> 1000 # (two "pieces of memory" has been used) 

But when you create a new reference to $a named $b then something like that happens:

$a = 1000; $b =& $a; # $a --\ # --> 1000 # $b --/ # (one "piece of memory" has been used but two different names ($a, $b) point on it) 

Comments

1

You will get the ans, if you follow the following code (i have add some lines)

$a=5; $b=6; $a=&$b; echo $a; //6 $b = 8; echo $a; //8 $a=5; $b=6; $a=$b; echo $a; //6 $b = 20; echo $a; //6 

with & simple, variable $a points the variable $b

without &, $b just copy into $a

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.