Asked  7 Months ago    Answers:  5   Viewed   46 times

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

Is it deprecated?



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.

Wednesday, March 31, 2021
answered 7 Months ago

It evaluates to the left operand if the left operand is truthy, and the right operand otherwise.

In pseudocode,

foo = bar ?: baz;

roughly resolves to

foo = bar ? bar : baz;


if (bar) {
    foo = bar;
} else {
    foo = baz;

with the difference that bar will only be evaluated once.

You can also use this to do a "self-check" of foo as demonstrated in the code example you posted:

foo = foo ?: bar;

This will assign bar to foo if foo is null or falsey, else it will leave foo unchanged.

Some more examples:

    var_dump(5 ?: 0); // 5
    var_dump(false ?: 0); // 0
    var_dump(null ?: 'foo'); // 'foo'
    var_dump(true ?: 123); // true
    var_dump('rock' ?: 'roll'); // 'rock'

By the way, it's called the Elvis operator.

Elvis operator

Wednesday, March 31, 2021
answered 7 Months ago

From the docs:

Coalesce equal or ??=operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one. If the value is not null, nothing is done.


// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';

So it's basically just a shorthand to assign a value if it hasn't been assigned before.

Wednesday, March 31, 2021
answered 7 Months ago

== means equality, so the conditional reads as:

If pre-incremented $x equals 10, echo $x

Single = is assignment, where a variable is set to contain a value:

$word = 'hello';
$number = 5;
// etc.

echo "I said $word $number times!";

Regarding the increment opperators:

You'll see things like ++$x and $i-- as you learn PHP (and/or other languages). These are increment/decrement operators. Where they're positioned in relation to the variable they're operating on is important.

If they're placed before the variable, like ++$x, it's a pre-increment/decrement. This means the operation is performed before anything else can be done to the variable. If it's placed after, like $x++, it's a post-increment/decrement, and it means that the operation is performed afterward.

It's easiest to see in an example script:

$x = 5;

echo ++$x; // 6
echo $x++; // ALSO 6
echo $x; // NOW 7
Saturday, August 7, 2021
answered 3 Months ago

My programming ruby book (2nd edition) also lists unary operators as having higher precedence than assignment.

The unary operator IS being given highest precedence. The reason the line is parsed as ~ (a = 1) is because decomposing the line into valid syntax is of higher precedence than anything else, including using the simple variable 'a' as the expression the unary operator operates on.

If the ruby parser could have made something valid of the rest of the line, it would have used (~ a), but there is no valid rule than matches = something, only lvalue '=' rvalue.

You can regard "valid syntax" as the top priority, then simple values, constant and variable names and then the standard operators under that.

Wednesday, September 29, 2021
answered 4 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :