125

What does the =& (equals-ampersand) assignment operator do in PHP?

Is it deprecated?

1

4 Answers 4

155

It's not deprecated and is unlikely to be. It's the standard way to, for example, make part of one array or object mirror changes made to another, instead of copying the existing data.

It's called assignment by reference, which, to quote the manual, "means that both variables end up pointing at the same data, and nothing is copied anywhere".

The only thing that is deprecated with =& is "assigning the result of new by reference" in PHP 5, which might be the source of any confusion. new is automatically assigned by reference, so & is redundant/deprecated in$o = &new C;, but not in $o = &$c;.


Since it's hard to search, note that =& (equals ampersand) is the same as = & (equals space ampersand) and is often written such that it runs into the other variable like $x = &$y['z']; or $x = &$someVar (ampersand dollar sign variable name). Example simplified from the docs:

$a = 3;
$b = &$a;
$a = 4;
print "$b"; // prints 4

Here's a handy link to a detailed section on Assign By Reference in the PHP manual. That page is part of a series on references - it's worth taking a minute to read the whole series.

1
  • 9
    side note re: "since it's hard to search": symbolhound.com is your friend for stuff like this. Commented Dec 13, 2016 at 21:24
26

It's two different operators. = is assignment as you probably know. And & means the variable should be accessed by reference rather than by value.

5
  • 67
    I'm sorry but this is way too simplistic. $a =& $b means to make the variable $a refer to the same thing that $b does right now. After this, $a = 5; will also result in $b having a 5. However the reference link may be broken by $b =& $xyz; or unset($b); At which time $a will be the only variable that knows where the cell is that holds the 5. Also beware that if you set $a using =&, you must use =& next time (or unset($a)) to change the reference link of $a, specifically $a = NULL; will not break the link, it only replaces the 5 with null;
    – Don
    Commented Apr 7, 2010 at 1:51
  • 8
    @Don: Thanks for the elaboration. I can tell you're a C programmer.
    – Asaph
    Commented Apr 7, 2010 at 3:26
  • 9
    I second what Don says. But I wouldn't say this is way too simplistic. I'd say this is wrong.
    – Artefacto
    Commented Aug 21, 2010 at 17:11
  • @Don you really should post your comment as an answer.
    – Adam
    Commented Jan 4, 2019 at 13:42
  • I agree with Artefacto, this answer is wrong and actively misleading. There is no such thing in PHP as "accessing a variable by reference", and assignment by reference is not the same operation as a normal assignment. I realise it was a long time ago that you wrote this answer, but can see that you're still active, so please can you have a look at this answer with more experienced eyes, and consider editing or deleting it if you agree that it is incorrect.
    – IMSoP
    Commented Apr 8, 2021 at 9:06
8

The symbol & is used in various ways in PHP to represent operations with "references". The PHP manual has a section titled References Explained which every PHP programmer should read.

It's important to understand that references in PHP are not a data type, like a pointer, but a concept regarding how variables work. There is therefore no single meaning of & - you should not read it as "make a reference" - it just means "something reference-y is happening here".

In particular, the syntax $a =& $b, which can also be written $a = &$b, represents assignment by reference. It binds two variables together, so that they both point at the same piece of data. Think of the & as modifying the = rather than modifying the $b.

Once you've bound two variables together in this way, they are interchangeable - you can't say that "$a points to $b" or "$b points to $a":

$a =& $b;
$a = 42;
// both $a and $b will be 42
$b = 101;
// both $a and $b will be 101

You can also link more than two variables together as references, and again it doesn't matter which of the existing names you use on the right-hand side of the assignment:

$a =& $b;
$c =& $b;
$d =& $a;
$e =& $c;
// $a, $b, $c, $d, and $e now all point to the same data, interchangeably

However, if you put the same variable on the left-hand side, it breaks the existing link of that variable, and links it to something else:

$a =& $b;
// $a and $b are linked together
$a =& $c;
// $a is now linked to $c
// the value of $b doesn't change, but it is not linked to $a or $c

To "break" the link without making a new link, you can use the unset keyword:

$a =& $b;
$c =& $a;
// $a, $b, and $c are all linked together
unset($a);
// $b and $c are still linked together, but $a is independent

Some descriptions refer to =& as "creating or adding to a reference set". Perhaps it would have been better if it had been implemented as a function, like bind($a, $b) to highlight that both arguments are affected by the operation.

7
$x = &$y['z'];

also has the effect of creating $y['z'] if it doesn't exist, and setting it to null.

This prevents error messages that you might have wanted to read. I haven't found documentation on this yet; possibly new in 5.3, for all I know.

1
  • For what it's worth, this behaviour existed at least as far back as PHP 4.3 3v4l.org/3g2WZ It's sometimes called "autovivification", but I don't know where in the manual it's described in detail.
    – IMSoP
    Commented Jun 28, 2022 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.