$i = $i++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    #1

    $i = $i++

    someone asked a question in alt.php about a problem they were having with an
    algorytm. it contained something to the effect of:

    $i = $i++;

    some example code they'd snatched somewhere. in a loop, the expected $i to
    increment. i explained why i thought it would not - as it does not. however,
    i want to make sure i gave a valid answer.

    anyone have input?


  • Jerry Stuckle

    #2
    Re: $i = $i++

    Steve wrote:
    someone asked a question in alt.php about a problem they were having with an
    algorytm. it contained something to the effect of:
    >
    $i = $i++;
    >
    some example code they'd snatched somewhere. in a loop, the expected $i to
    increment. i explained why i thought it would not - as it does not. however,
    i want to make sure i gave a valid answer.
    >
    anyone have input?
    >
    >
    $i is incremented. Then $i is set with the old (pre-increment) value of
    $i (++ has precedence over =).



    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Andy Hassall

      #3
      Re: $i = $i++

      On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example .comwrote:
      >someone asked a question in alt.php about a problem they were having with an
      >algorytm. it contained something to the effect of:
      >
      >$i = $i++;
      >
      >some example code they'd snatched somewhere. in a loop, the expected $i to
      >increment. i explained why i thought it would not - as it does not. however,
      >i want to make sure i gave a valid answer.
      >
      >anyone have input?
      In C and C++ that's undefined. PHP is less formally defined and so it is even
      more daft to try it.



      Comment

      • Anonymous

        #4
        Re: $i = $i++

        Steve wrote:
        >
        someone asked a question in alt.php about a problem they were having with an
        algorytm. it contained something to the effect of:
        >
        $i = $i++;
        >
        some example code they'd snatched somewhere. in a loop, the expected $i to
        increment. i explained why i thought it would not - as it does not. however,
        i want to make sure i gave a valid answer.
        >
        anyone have input?
        I don't read alt.php so I don't know what you wrote, but the answer is
        pretty obvious.

        First, the ++ operators are increment operators. They execute an
        incrementation of the variable by 1. $i++ by itself already executes $i
        = $i + 1, which the original poster most likely wanted to do. But the
        increment operators also return a value for further usage. The
        post-increment operator returns the value before the variable was
        incremented, the pre-increment operator returns the value after
        incrementation.

        $i++ is post-increment. That means, first the content of the variable is
        evaluated, then it is incremented, then the evaluated (meaning the
        former) value is passed to the equation which gets executed and resets
        $i to its former value.

        In other words, what the line $i=$i++; actually does is:

        $evaluated = $i; //first save the current value for later usage
        $i = $i + 1; // execute the increment after that because we are doing
        post-increment
        $i = $evaluated; // execute the actual equation

        So I'd be very surprised if $i would change. :-)

        Imagine the code:

        $a = 1;
        $b = $a++;

        Then $a would be 2 and $b would be 1. If you wanted $b to get the new
        value of $a and not the old one you would have to use pre-increment. The
        variable would first be incremented and the new value passed to the
        equation. Pre-increment would be ++$a instead of $a++. And if you wanted
        $b to be just 1 larger than $a without changing $a at all you would
        write $b = $a + 1;.

        Pretty simple once you understand the concept of the increment (or
        decrement) operators. If you (respectively the original posters) are
        still unsure about them you should read them up in chapter 14 und 15 of
        the PHP manual. They give several examples for using them. Feel free to
        forward my explanation to alt.php, Steve.

        Bye!

        Comment

        • Tyno Gendo

          #5
          Re: $i = $i++

          Andy Hassall wrote:
          On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example .comwrote:
          >
          >someone asked a question in alt.php about a problem they were having with an
          >algorytm. it contained something to the effect of:
          >>
          >$i = $i++;
          >>
          >some example code they'd snatched somewhere. in a loop, the expected $i to
          >increment. i explained why i thought it would not - as it does not. however,
          >i want to make sure i gave a valid answer.
          >>
          >anyone have input?
          >
          In C and C++ that's undefined. PHP is less formally defined and so it is even
          more daft to try it.
          >

          http://c-faq.com/expr/ieqiplusplus.html
          Yes, definately a syntax to avoid, seems unpredictable at best...

          <?php
          $i = 2;
          echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment

          $i = 2;
          echo $i++ + $i; // expect 5, gives 5, applies ++ increment

          $i = 2;
          echo $i++ + $i++ + 1; // expect 7, get 6
          ?>

          Comment

          • Tyno Gendo

            #6
            Re: $i = $i++

            Anonymous wrote:
            Steve wrote:
            >someone asked a question in alt.php about a problem they were having with an
            >algorytm. it contained something to the effect of:
            >>
            >$i = $i++;
            >>
            >some example code they'd snatched somewhere. in a loop, the expected $i to
            >increment. i explained why i thought it would not - as it does not. however,
            >i want to make sure i gave a valid answer.
            >>
            >anyone have input?
            >
            I don't read alt.php so I don't know what you wrote, but the answer is
            pretty obvious.
            >
            First, the ++ operators are increment operators. They execute an
            incrementation of the variable by 1. $i++ by itself already executes $i
            = $i + 1, which the original poster most likely wanted to do. But the
            increment operators also return a value for further usage. The
            post-increment operator returns the value before the variable was
            incremented, the pre-increment operator returns the value after
            incrementation.
            >
            $i++ is post-increment. That means, first the content of the variable is
            evaluated, then it is incremented, then the evaluated (meaning the
            former) value is passed to the equation which gets executed and resets
            $i to its former value.
            >
            In other words, what the line $i=$i++; actually does is:
            >
            $evaluated = $i; //first save the current value for later usage
            $i = $i + 1; // execute the increment after that because we are doing
            post-increment
            $i = $evaluated; // execute the actual equation
            >
            So I'd be very surprised if $i would change. :-)
            >
            Imagine the code:
            >
            $a = 1;
            $b = $a++;
            >
            Then $a would be 2 and $b would be 1. If you wanted $b to get the new
            value of $a and not the old one you would have to use pre-increment. The
            variable would first be incremented and the new value passed to the
            equation. Pre-increment would be ++$a instead of $a++. And if you wanted
            $b to be just 1 larger than $a without changing $a at all you would
            write $b = $a + 1;.
            >
            Pretty simple once you understand the concept of the increment (or
            decrement) operators. If you (respectively the original posters) are
            still unsure about them you should read them up in chapter 14 und 15 of
            the PHP manual. They give several examples for using them. Feel free to
            forward my explanation to alt.php, Steve.
            >
            Bye!
            Ah, right, I see.. so from my previous post...

            $i = 2;
            echo $i++ + $i; // expect 5, gives 5, applies ++ increment

            i complete messed up on that one...

            but seeing your explanation i can see this gives

            2 (return value of $i++) + 3 (now incremented value) = 5

            it all makes sense, in a non-sensical fashion, avoid this syntax :)

            Comment

            • Gordon Burditt

              #7
              Re: $i = $i++

              >Yes, definately a syntax to avoid, seems unpredictable at best...
              >
              ><?php
              $i = 2;
              echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
              Please get your expecter fixed. $i++ returns the value *BEFORE*
              the increment. (If you want ++$i, you know where to find it.) This
              one is unambiguous since all possible orders come up with the same
              result.

              For the other expressions involving $i++ and another reference to $i's
              value to give a resource returning Donald Trump, who's going to find the
              programmer and say "You're Fired".

              Comment

              • Anonymous

                #8
                Re: $i = $i++

                Yes, definately a syntax to avoid, seems unpredictable at best...

                No, I would always expect what you got, it's perfectly predictable. You
                should read up how the ++ operators work.
                >
                <?php
                $i = 2;
                echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
                Does not ignore the increment, but $i++ returns the value before
                incrementation, then increments. So $i++ + 1 correctly returns 2 + 1,
                which is 3.
                $i = 2;
                echo $i++ + $i; // expect 5, gives 5, applies ++ increment
                Correct. First $i++ is executed which returns 2, then increments a to 3,
                then the + $i will be evaluated, so the expression will be 2 + 3, which
                is 5.
                $i = 2;
                echo $i++ + $i++ + 1; // expect 7, get 6
                ?>
                Why do you expect 7? First $i++ returns 2, increments $i to 3, so the
                expression will be 2 + $i++ + 1, with $i = 3, the second $i++ will
                return 3 and increment $i to 4, so we have 2 + 3 + 1, which equals 6.

                Bye!

                Comment

                • Tyno Gendo

                  #9
                  Re: $i = $i++

                  Gordon Burditt wrote:
                  >Yes, definately a syntax to avoid, seems unpredictable at best...
                  >>
                  ><?php
                  > $i = 2;
                  > echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
                  >
                  Please get your expecter fixed. $i++ returns the value *BEFORE*
                  the increment. (If you want ++$i, you know where to find it.) This
                  one is unambiguous since all possible orders come up with the same
                  result.
                  >
                  For the other expressions involving $i++ and another reference to $i's
                  value to give a resource returning Donald Trump, who's going to find the
                  programmer and say "You're Fired".
                  >
                  Yes i can see that, and I'd ever use this crazy syntax anyway, but the
                  "human" way of reading this would seem to make it make sense so you can
                  understand how easy it is to be misinterpreted.

                  to the average bod....

                  $i++ would say 2+1 which is 3 (shock), yes... + 1, yep, 4, ERM... NO.

                  crazy computers, they do the most crazy things ;)

                  Comment

                  • Tyno Gendo

                    #10
                    Re: $i = $i++

                    Anonymous wrote:
                    >Yes, definately a syntax to avoid, seems unpredictable at best...
                    >
                    No, I would always expect what you got, it's perfectly predictable. You
                    should read up how the ++ operators work.
                    >
                    ><?php
                    > $i = 2;
                    > echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
                    >
                    Does not ignore the increment, but $i++ returns the value before
                    incrementation, then increments. So $i++ + 1 correctly returns 2 + 1,
                    which is 3.
                    >
                    > $i = 2;
                    > echo $i++ + $i; // expect 5, gives 5, applies ++ increment
                    >
                    Correct. First $i++ is executed which returns 2, then increments a to 3,
                    then the + $i will be evaluated, so the expression will be 2 + 3, which
                    is 5.
                    >
                    > $i = 2;
                    > echo $i++ + $i++ + 1; // expect 7, get 6
                    >?>
                    >
                    Why do you expect 7? First $i++ returns 2, increments $i to 3, so the
                    expression will be 2 + $i++ + 1, with $i = 3, the second $i++ will
                    return 3 and increment $i to 4, so we have 2 + 3 + 1, which equals 6.
                    >
                    Bye!
                    I have now, and it makes sense...

                    I'm glad this thread came up on alt.php as I'd never even considered
                    doing the $i = $i++ before someone posted it on there, but now I've seen
                    someone do it then I won't make the same mistake they did, and I didn't
                    realise that a '++' first returned the original value as if it was a
                    function...

                    But clearly, the PHP man says it does:

                    "$a++ Post-increment Returns $a, then increments $a by one."

                    Comment

                    • Jerry Stuckle

                      #11
                      Re: $i = $i++

                      Gordon Burditt wrote:
                      >Yes, definately a syntax to avoid, seems unpredictable at best...
                      >>
                      ><?php
                      > $i = 2;
                      > echo $i++ + 1; /// i'd expect 4, gives 3, ignores ++ increment
                      >
                      Please get your expecter fixed. $i++ returns the value *BEFORE*
                      the increment. (If you want ++$i, you know where to find it.) This
                      one is unambiguous since all possible orders come up with the same
                      result.
                      >
                      For the other expressions involving $i++ and another reference to $i's
                      value to give a resource returning Donald Trump, who's going to find the
                      programmer and say "You're Fired".
                      >
                      Actually, not entirely true.

                      <?
                      $i = 2;
                      echo $i++ + $i++ + 1; // expect 7, get 6
                      ?>

                      can return either 5 or 6.

                      The reason here is - the order of operations is guaranteed, but not the
                      order the *operands* are evaluated.

                      For instance - the first $i++ is incremented to 3 and the old value (2)
                      returned. But there is no guarantee the value of the second $i++ will
                      be evaluated before or after the first $i++ has been processed.

                      This might be:

                      2 + 3 + 1

                      but

                      2 + 2 + 1

                      could also be valid, as could

                      3 + 2 + 1

                      Or course the first and third return the same value - but only because
                      it's addition.

                      In either case, however, $i will be incremented to 4 at the end.

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: $i = $i++

                        Andy Hassall wrote:
                        On Wed, 11 Apr 2007 16:20:45 -0500, "Steve" <no.one@example .comwrote:
                        >
                        >someone asked a question in alt.php about a problem they were having with an
                        >algorytm. it contained something to the effect of:
                        >>
                        >$i = $i++;
                        >>
                        >some example code they'd snatched somewhere. in a loop, the expected $i to
                        >increment. i explained why i thought it would not - as it does not. however,
                        >i want to make sure i gave a valid answer.
                        >>
                        >anyone have input?
                        >
                        In C and C++ that's undefined. PHP is less formally defined and so it is even
                        more daft to try it.
                        >

                        http://c-faq.com/expr/ieqiplusplus.html
                        Andy,

                        Actually, in both C and C++ this special case is defined. However, a
                        number of other cases aren't - i.e.

                        $i = 2;
                        func($i++, $i++);

                        could pass (2, 3), (3, 2) or even (2, 2) - depending on how the operands
                        are processed.

                        Remember - operators have precedence, but this does not generally apply
                        to operands. Assignment is a special case as it is always processed
                        last (other than the comma operator).


                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        • Steve

                          #13
                          Re: $i = $i++

                          On Wed, 11 Apr 2007 22:42:24 +0000, Gordon Burditt wrote:
                          For the other expressions involving $i++ and another reference to $i's
                          value to give a resource returning Donald Trump, who's going to find the
                          programmer and say "You're Fired".
                          ....is the correct answer!

                          Comment

                          • Toby A Inkster

                            #14
                            Re: $i = $i++

                            Tyno Gendo wrote:
                            I didn't realise that a '++' first returned the original value as if it
                            was a function...
                            Post-increment vs pre-increment:

                            ++$i increments then returns value
                            $i++ returns value then increments

                            --
                            Toby A Inkster BSc (Hons) ARCS
                            Contact Me ~ http://tobyinkster.co.uk/contact
                            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

                            * = I'm getting there!

                            Comment

                            • Tyno Gendo

                              #15
                              Re: $i = $i++

                              Steve wrote:
                              On Wed, 11 Apr 2007 22:42:24 +0000, Gordon Burditt wrote:
                              >
                              >For the other expressions involving $i++ and another reference to $i's
                              >value to give a resource returning Donald Trump, who's going to find the
                              >programmer and say "You're Fired".
                              >
                              ...is the correct answer!
                              I'll get my coat.

                              Comment

                              Working...