Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the PHP operator =& mean? [duplicate]

Tags:

operators

php

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);
        ....
like image 834
IberoMedia Avatar asked Aug 19 '10 21:08

IberoMedia


People also ask

What does the :: operator do in PHP?

In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.

What does this symbol mean in PHP?

This operator allows for simpler three-way comparison between left-hand and right-hand operands. The operator results in an integer expression of: 0 when both operands are equal. Less than 0 when the left-hand operand is less than the right-hand operand.

What does === mean in PHP?

=== Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

What does && means in PHP?

PHP && Operator The logical operator && returns: TRUE only if both of its operands evaluate to true. FALSE if either or both of its operands evaluate to false.


3 Answers

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.

like image 120
Austin Hyde Avatar answered Oct 09 '22 11:10

Austin Hyde


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.

like image 43
Jacob Relkin Avatar answered Oct 09 '22 12:10

Jacob Relkin


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.

like image 29
bschaeffer Avatar answered Oct 09 '22 11:10

bschaeffer