What is the difference between the two operators?
Thank you very much in advanced.
| SitePoint Sponsor |
What is the difference between the two operators?
Thank you very much in advanced.
I Love JavaScript & jQuery...
There is no "=-" in PHP.
Ooops sorry wrong character.
It suppose to be += vs =+ .
Thanks in advanced.
I Love JavaScript & jQuery...

There's no =+ in php either...
http://php.net/manual/en/language.op...assignment.php


Do the following exercises:
See how the values differ?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 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).


thanks guys.
I Love JavaScript & jQuery...


Just for the record, @logic_earth ; is correct that =- doesn't exist.
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.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);
The latter works as expected and $b contains negative 4

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.)
"Folks who know what they're doing make complexity seem simple."


That could be... (odd seeing how C/C++ accepts these out of the box and PHP is derived from them).
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 ++iPHP 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);
"Folks who know what they're doing make complexity seem simple."


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.
Code:<?php $a = -5; $b = - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - + + + - - - - - - - - - - - - + + + - - - - - - - - - - - - + + + - - + - - - - - - - - - + + + - - + + + - - - - - - - - - + + + - - + + + - - - - - - - - - + + + - - + + + - - - - - - - - - + - - + + + - - - - - - - - - - - - + + + - - - - - - - - - - - - + + + - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $a; var_dump($a, $b);
Bookmarks