# += v.s. =-?

What is the difference between the two operators?

Thank you very much in advanced.

There is no “=-” in PHP.

Ooops sorry wrong character.

It suppose to be += vs =+ .

There’s no =+ in php either…

http://php.net/manual/en/language.operators.assignment.php

Do the following exercises:

``````\$a = 5;
\$b = 1;
\$c = 0;
\$b =+ \$c = \$a;
var_dump(\$a, \$b, \$c);

\$a = 5;
\$b = 1;
\$c = 0;
\$b += \$c = \$a;
var_dump(\$a, \$b, \$c);
``````

See how the values differ?

In the latter, the addition happens before the assignment of \$b, so 1 + 5 gets executed, then 6 is assigned to \$b. In the former, the assignment happens first, 5 gets assigned to \$b, then the addition occurs (hence \$b does not store 6).

I beg to differ. That logic works just fine and performs exactly how C, C++ and C# perform it. You’ll also find this to occur using \$a++ and ++\$a.

``````\$a = 5;
\$b = \$a++;
var_dump(\$a, \$b);

\$a = 5;
\$b = ++\$a;
var_dump(\$a, \$b);
``````

thanks guys.

Just for the record, @logic_earth ; is correct that =- doesn’t exist.

``````\$a = 5;
\$b = 1;
\$c = 0;
\$b =- \$c = \$a;
var_dump(\$a, \$b, \$c);

\$a = 5;
\$b = 1;
\$c = 0;
\$b -= \$c = \$a;
var_dump(\$a, \$b, \$c);
``````

In the first set, I would have expected \$b to be 5, not negative 5. So PHP is treating it as (-1)\$c, which I find interesting.
The latter works as expected and \$b contains negative 4

I stand corrected - though, it’s not documented, at least not on the official site…

Actually, I think DaveMaxwell is right. There is no =+ operator. And since there isn’t, the “+” in this case is treated as the unary “+”. That’s why all the values come out “5”, because it’s the same as \$b = \$c = \$a;. (The only side effect of the unary “+” would be to convert the value that’s assigned to \$b to an int if it wasn’t already.)

That could be… (odd seeing how C/C++ accepts these out of the box and PHP is derived from them).

``````\$a = -5;
\$b = 1;
\$c = 0;
\$d = \$b =+ \$c = \$a;
var_dump(\$a, \$b, \$c, \$d);

\$a = -5;
\$b = 1;
\$c = 0;
\$d = \$b += \$c = \$a;
var_dump(\$a, \$b, \$c, \$d);
``````

As that gives -5 to \$d to the former (still) and -4 in the latter. I would have expected -4 for both. At least now the OP knows how other languages deal with += and =+, -= and =-, and i++ versus ++i

I’m not so sure that it’s a C/C++ operator either. You can look up documentation for C++ operators, and =+ isn’t one of them. Most likely C++ treats it the same way PHP does: an assignment operator followed by a unary plus operator.

Hmm… I wonder where I’ve run into that before then… how strange, I’m sure I run into it as we did assignment tests to identify why/how the two differed. I could have sworn it was in a C++ class, but maybe not… strange.

Doh! Just figured out why I recall =+ and =-. Our assignment was to create those operators to perform assignment first, then addition of the right side. Took me a while to track down that (considering I did it over 8 years ago, wonder why it stuck in my mind for so long…)

C, C++, and C# seem to treat the + after the = as a unary/binary operator (per Jeff’s remark).

Of the two separate operators in play (assignment, and unary positive) only one is documented in the manual. I cannot find anything saying that the unary positive is deliberately not included in the docs.

However, I would assume that it is not there purely because it is an almost useless operator, there because it has to be rather than because it is super-useful; the only potential need I can think of would be to make a value numeric without having to cast (and being restricted to only one type) using `(int)` or `(float)`.

To take things to silly lengths to show unary operators in action, the following is perfectly valid PHP. Bear in mind, there is no addition or subtraction occurring.

``````
<?php
\$a = -5;
\$b = [COLOR=#CCCCCC]- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -
- - - - - - - [COLOR=#E27E32]+[/COLOR] - - - - - - - -
- - - - - - [COLOR=#E27E32]+ + +[/COLOR] - - - - - - -
- - - - - [COLOR=#E27E32]+ + +[/COLOR] - - - - - - - -
- - - - [COLOR=#E27E32]+ + +[/COLOR] - - [COLOR=#006AAA]+[/COLOR] - - - - - -
- - - [COLOR=#E27E32]+ + +[/COLOR] - - [COLOR=#006AAA]+ + +[/COLOR] - - - - -
- - - - [COLOR=#E27E32]+ + +[/COLOR] - - [COLOR=#006AAA]+ + +[/COLOR] - - - -
- - - - - [COLOR=#E27E32]+ + +[/COLOR] - - [COLOR=#006AAA]+ + +[/COLOR] - - -
- - - - - - [COLOR=#E27E32]+[/COLOR] - - [COLOR=#006AAA]+ + +[/COLOR] - - - -
- - - - - - - - [COLOR=#006AAA]+ + +[/COLOR] - - - - -
- - - - - - - [COLOR=#006AAA]+ + +[/COLOR] - - - - - -
- - - - - - - - [COLOR=#006AAA]+[/COLOR] - - - - - - -
- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -[/COLOR]

\$a;

var_dump(\$a, \$b);

``````