37

Possible Duplicate: What do the "=&" and "&=" operators in PHP mean?

I found the operator "=&" in the following code, and I do not know what it means. What does it mean and what does it do?

The code where I read it:

function ContentParseRoute($segments)
{
    $vars = array();

    //Get the active menu item
    $menu =& JSite::getMenu();
    $item =& $menu->getActive();

    // Count route segments
    $count = count($segments);
        ....
Community
  • 1
  • 1
IberoMedia
  • 2,226
  • 7
  • 36
  • 61

3 Answers3

62

This isn't an assignment (=) by reference (&).

If you were to say:

$a = 42;
$b =& $a;

You are actually saying assign $a by reference to $b.

What assigning by reference does is "tie" the two variables together. Now, if you were to modify $a later on, $b would change with it.

For example:

$a = 42;
$b =& $a;

//later
echo $a; // 42
echo $b; // 42

$a = 13;
echo $a; // 13
echo $b; // 13

EDIT:

As Artefacto points out in the comments, $a =& $b is not the same as $a = (&$b).

This is because while the & operator means make a reference out of something, the = operator does assign-by-value, so the expression $a = (&$b) means make a temporary reference to $b, then assign the value of that temporary to $a, which is not assign-by-reference.

Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
  • 11
    -1 `$b = (&$a);` is invalid syntax. I know that's not your point, but `=&` is actually a single operator, and translates to the opcode `ASSIGN_REF`. This is not an irrelevant internals observation. Consider a function `func` that returns by reference `$a = func()`. This function is returning a reference, yet `$a` won't be a reference because the operator `=` breaks the reference set and clears the flag. You have to actually do `$a =& $func()`. `&` means "make a reference out of this (e.g. `array(&$a)`), but making a reference is not enough to assign by reference. – Artefacto Aug 20 '10 at 01:26
  • 4
    Really, now? I did not know that, and it never broke for me before (at least apparently). Guess you learn something every day. :) I'll edit my answer for this correction. – Austin Hyde Aug 20 '10 at 13:34
  • 3
    Should that be "This is an assignment by reference"? "You are actually saying assign $b by reference to $a" also feels a more intuitive way round to me. – And Finally Apr 12 '18 at 09:31
7

It is the referential assignment operator.

This means that when you modify the LHS of the operator later on in code, it will modify the RHS. You are pointing the LHS to the same block of memory that the RHS occupies.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 4
    LHS = Left Hand Side RHS = Right Hand Side – Htbaa Aug 19 '10 at 21:57
  • This is correct (at least you didn't say "these are actually two operators"). However, the last phrase is a bit misleading. In `$a = $b;`, `$b` will, in most cases, also point to the same memory block as `$a`. The difference is that the variable will be separated on modification, while if it is in a reference set, it will. It may be more accurate to say `The symbols on the LHS and the RHS are the same variable" or something to that effect, and leave out the "memory" reference. – Artefacto Aug 20 '10 at 01:33
  • The accepted answer seems to imply that when you modify the RHS, the LHS also gets modified. Is that correct? – Kim Stacks Feb 14 '15 at 15:21
  • Does "modify" mean that `$lhs=&$a; $lhs='yes'; echo $a;` prints "yes"? and Does "modify" mean that `$lhs=&$a; $a='yes'; echo $lhs;` prints "yes"? – David Spector Jan 28 '23 at 15:41
5

Here's an example of it in use:

$array = array('apple', 'orange', 'banana');

// Without &
foreach($array as $d)
{
    $d = 'fruit';
}

echo implode(', ', $array); // apple, orange, banana

// With &
foreach($array as &$d)
{
    $d = 'fruit';
}

echo implode(', ', $array); // fruit, fruit, fruit

Not an explanation, but an example of being able to use the & operator without using it in an =& assignment.

bschaeffer
  • 2,824
  • 1
  • 29
  • 59
  • 1
    See my comments on the other answers. Don't be confused by several meaning of `&`. They're all related to references somehow, but they don't mean the same thing. – Artefacto Aug 20 '10 at 01:34