views:

89

answers:

6

Hi guys.. 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
+10  A: 

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
Wrikken
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.
Liam Bailey
+1  A: 

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;

?>
PIM
A: 

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

Sadat
A: 

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"
Gumbo
A: 

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.
slightlymore
A: 

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)
Crozin