Coder Perfect

=& is a PHP reference assignment operator.


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

Is it deprecated?

Asked by Kyle J. Dye

Solution #1

It hasn’t been deprecated yet and isn’t likely to be. Instead of replicating the existing data, it’s the typical technique to make part of one array or object mirror changes made to another.

It’s called assignment by reference, and it “means that both variables end up pointing at the same data, and nothing is copied anywhere,” according to the documentation.

Only “assigning the result of new by reference” in PHP 5 is deprecated with =&, which could be the source of any confusion. Because new is automatically allocated by reference in $o = &new C;, but not in $o = &$c;, & is redundant/deprecated.

Because searching is difficult, keep in mind that =& (equals ampersand) is the same as = & (equals space ampersand) and is frequently expressed as $x = &$y[‘z’]; or $x = &$someVar (ampersand dollar sign variable name). As an example, here’s a simplified version of what’s in the docs:

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

Here’s a link to the PHP manual’s detailed section on Assign By Reference. That page is part of a series on references; it’s well worth your time to read through the entire series.

Answered by user56reinstatemonica8

Solution #2

There are two distinct operators. As you probably know, = stands for assignment. The & symbol indicates that the variable should be accessed by reference rather than value.

Answered by Asaph

Solution #3

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

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

This blocks you from seeing error messages that you might want to read. I haven’t found any documentation on this yet; it could be new in 5.3.

Answered by malcanso

Solution #4

In PHP, the symbol & is used to express operations using “references” in a variety of ways. Every PHP coder should read the section titled References Explained in the PHP manual.

It’s crucial to realize that in PHP, references aren’t a data type like a pointer, but rather an idea about how variables function. As a result, there is no single meaning of &; it does not mean “create a reference,” but rather “something reference-y is going on here.”

Assignment by reference is represented by the syntax $a =& $b, which can also be written $a = &$b. It connects two variables so that they both point to the same piece of information.

You can’t say “$a points to $b” or “$b points to $a” if you’ve tied two variables together in this way: they’re interchangeable.

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

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

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

When you put the same variable on the left-hand side, though, it breaks the present link and connects 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

You can use the unset keyword to “break” the link without creating a new one:

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

Answered by IMSoP

Solution #5

I’d want to call attention to “Assigning By Reference’s” semantics and code aesthetics. The first statement of the OP suggests a misunderstanding:

First, have a look at the Assignment Operators portion of the PHP Docs page. It’s worth noting that the = precedes the & and that the two symbols are separated. This is because they are NOT “combined operators”. Semantically, it is “assigning” a “reference”; it is not a “reference assignment operator”.

Second, have a look at how ALL of the “combined operators” are written in the documentation section. The right-most symbol is always the =. This is a crucial distinction since putting the & to the left of the = transforms it into a combination operator (“bitwise and assignment operator”) rather than a reference assignment.

All PHP developers should be aware of the PSR coding standards and attempt to follow them. Take note of the following regulation from PSR-12 Section 6.2:

According to this rule, there must always be a space after the = operator; =& is a violation.

There are also rules that indicate that there should be no space between & and its variable, argument, function, or whatever.


Demonstrated consistently/correctly:

Not demonstrated consistently/correctly:

Answered by mickmackusa

Post is based on