SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: += v.s. =- ?

  1. #1
    SitePoint Guru solidcodes's Avatar
    Join Date
    Jul 2006
    Posts
    611
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    += v.s. =- ?

    What is the difference between the two operators?

    Thank you very much in advanced.
    I Love JavaScript & jQuery...

  2. #2
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    8,974
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    There is no "=-" in PHP.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru solidcodes's Avatar
    Join Date
    Jul 2006
    Posts
    611
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooops sorry wrong character.

    It suppose to be += vs =+ .

    Thanks in advanced.
    I Love JavaScript & jQuery...

  4. #4
    Just Blow it! bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    6,680
    Mentioned
    54 Post(s)
    Tagged
    1 Thread(s)
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  5. #5
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,770
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    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).

  6. #6
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,770
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    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); 

  7. #7
    SitePoint Guru solidcodes's Avatar
    Join Date
    Jul 2006
    Posts
    611
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys.
    I Love JavaScript & jQuery...

  8. #8
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,770
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    Just Blow it! bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    6,680
    Mentioned
    54 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cpradio View Post
    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....
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  10. #10
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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.)
    "Folks who know what they're doing make complexity seem simple."

  11. #11
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,770
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    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

  12. #12
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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.
    "Folks who know what they're doing make complexity seem simple."

  13. #13
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,770
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    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.

  14. #14
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,770
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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).

  15. #15
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,353
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    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);
    Salathe
    Software Developer and PHP Documentation Team.

Bookmarks

Posting Permissions

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