简体   繁体   中英

What is the difference between “=” and “=&” when assigning variables?

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

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

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;

?>

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

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"

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM