# Thread: += v.s. =- ?

1. ## += v.s. =- ?

What is the difference between the two operators?

Thank you very much in advanced.

2. There is no "=-" in PHP.

3. Ooops sorry wrong character.

It suppose to be += vs =+ .

4. Do the following exercises:
PHP Code:
``` \$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).

5. Originally Posted by DaveMaxwell
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.

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

6. thanks guys.

7. Just for the record, @logic_earth ; is correct that =- doesn't exist.
PHP Code:
``` \$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 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.

PHP Code:
``` \$a = 5;\$b = \$a++;var_dump(\$a, \$b);\$a = 5;\$b = ++\$a;var_dump(\$a, \$b);  ```
I stand corrected - though, it's not documented, at least not on the official site....

Do the following exercises:
PHP Code:
``` \$a = 5;\$b = 1;\$c = 0;\$b =+ \$c = \$a;var_dump(\$a, \$b, \$c);  ```
... the assignment happens first, 5 gets assigned to \$b, then the addition occurs (hence \$b does not store 6).
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.)

10. That could be... (odd seeing how C/C++ accepts these out of the box and PHP is derived from them).
PHP Code:
``` \$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

That could be... (odd seeing how C/C++ accepts these out of the box and PHP is derived from them).
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.

12. 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.

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).

14. Originally Posted by DaveMaxwell
I stand corrected - though, it's not documented, at least not on the official site....
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.

Code:
```<?php
\$a = -5;
\$b = - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -
- - - - - - - + - - - - - - - -
- - - - - - + + + - - - - - - -
- - - - - + + + - - - - - - - -
- - - - + + + - - + - - - - - -
- - - + + + - - + + + - - - - -
- - - - + + + - - + + + - - - -
- - - - - + + + - - + + + - - -
- - - - - - + - - + + + - - - -
- - - - - - - - + + + - - - - -
- - - - - - - + + + - - - - - -
- - - - - - - - + - - - - - - -
- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -

\$a;

var_dump(\$a, \$b);```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•