SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: += v.s. =- ?

  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    += v.s. =- ?

    What is the difference between the two operators?

    Thank you very much in advanced.
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 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
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ooops sorry wrong character.

    It suppose to be += vs =+ .

    Thanks in advanced.
    Quality codes are optimized and tested...
    Click here for inspiration..

  4. #4
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,290
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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 Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks guys.
    Quality codes are optimized and tested...
    Click here for inspiration..

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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
    7,290
    Mentioned
    122 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
    Learn how to be ready for The Forums' Move to Discourse

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 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.)
    "First make it work. Then make it better."

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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 Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 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.
    "First make it work. Then make it better."

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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 Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 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,397
    Mentioned
    65 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 Manual Author.


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
  •