What does the =&
(equals-ampersand) assignment operator do in PHP?
Is it deprecated?
What does the =&
(equals-ampersand) assignment operator do in PHP?
Is it deprecated?
You can see examples about this behaviour in Logical Operators
Also you can read artical about Short-circuit evaluation
The short-circuit expression
x Sand y
(using Sand to denote the short-circuit variety) is equivalent to the conditional expressionif x then y else false;
the expressionx Sor y
is equivalent toif x then true else y
.
In php.
return x() and y();
equal to
if (x())
return (bool)y();
else
return false;
return x() or y();
equal to
if (x())
return true;
else
return (bool)y();
So, deal is not just in precedence.
Not returning a reference is a waste of resources and a yields a weird design. Why do you want to do a copy for all users of your operator even if almost all of them will discard that value?
a = b; // huh, why does this create an unnecessary copy?
In addition, it would be surprising to users of your class, since the built-in assignment operator doesn't copy likewise
int &a = (some_int = 0); // works
The short answer is - it isn't defined to work with strings.
Longer answer: if you try the subtraction operator on two strings, it will first cast them to numbers and then perform the arithmetic.
"10" - "2" = 8
If you try something that is non-numeric, you get a NaN related error:
"AA" - "A" = NaN
The parameter of an overloaded assignment operator can be any type and it can be passed by reference or by value (well, if the type is not copy constructible, then it can't be passed by value, obviously).
So, for example, you could have an assignment operator that takes an int
as a parameter:
MyClass& operator=(int);
The copy assignment operator is a special case of an assignment operator. It is any assignment operator that takes the same type as the class, either by value or by reference (the reference may be const- or volatile-qualified).
If you do not explicitly implement some form of the copy assignment operator, then one is implicitly declared and implemented by the compiler.
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 ofnew
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: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.