$i = $i++

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

    #16
    Re: $i = $i++


    "Tyno Gendo" <you@localhostw rote in message
    news:tIGdneRI04 3Xd4DbnZ2dnUVZ8 sfinZ2d@bt.com. ..
    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.
    >
    LOL
    I like your attitude Tyno!!


    Comment

    • David T. Ashley

      #17
      Re: $i = $i++

      "Andy Hassall" <andy@andyh.co. ukwrote in message
      news:63nq13tlvg 0fvp5i0lg54uvlo 18nhi5n3f@4ax.c om...
      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
      I've never really understood why

      $i = $i++;

      _needs_ to be undefined in C, C++, or PHP. It seems that in order to assign
      the left side, one needs to evaluate the right side, and since ++ has to
      bind tighter than = ... it seems there should be an unequivocal "right"
      answer ... $i will be unchanged.

      Nonetheless, I do agree that it IS undefined, i.e.



      When I think of undefined, I think of something like:

      my_function($i+ +, $i, $i--);

      where for example the C language standard says that the order of evaluation
      of function arguments is undefined.
      --
      David T. Ashley (dta@e3ft.com)
      http://www.e3ft.com (Consulting Home Page)
      http://www.dtashley.com (Personal Home Page)
      http://gpl.e3ft.com (GPL Publications and Projects)


      Comment

      • Steve

        #18
        Re: $i = $i++


        "Steve" <no.one@example .comwrote in message
        news:JgcTh.1016 $AE5.731@newsfe 06.lga...
        | 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?


        thanks all. that's how i explained it. i didn't quite get why the value of
        $i remained the same (whatever it's initial value was)...until:

        $i is incremented. Then $i is set with the old (pre-increment) value of
        $i (++ has precedence over =).

        thanks jerry. it just snapped for me. it's that even though $i++ may
        increment, the lhs assignment is made from $i's initial value prior to the
        increment... it's the = that resets/nullifies the ++ in this case. that
        about right?


        Comment

        • Rami Elomaa

          #19
          Re: $i = $i++

          Steve kirjoitti:
          "Steve" <no.one@example .comwrote in message
          news:JgcTh.1016 $AE5.731@newsfe 06.lga...
          | 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?
          >
          >
          thanks all. that's how i explained it. i didn't quite get why the value of
          $i remained the same (whatever it's initial value was)...until:
          >
          $i is incremented. Then $i is set with the old (pre-increment) value of
          $i (++ has precedence over =).
          >
          thanks jerry. it just snapped for me. it's that even though $i++ may
          increment, the lhs assignment is made from $i's initial value prior to the
          increment... it's the = that resets/nullifies the ++ in this case. that
          about right?
          Yes. That's why the assignment is not used with $i++. $i++ is a
          shorthand that both increments and assigns at the same time. If you go
          and add another assignment then it's fucked.

          $i = ++$i; // this would've worked as expected, though.

          Maybe this helps:

          <?php

          function ipp(&$i){ // equivalent of $i++
          $old_i = $i; // old value is stored in temporary location
          $i=$i+1; // value is altered
          return $old_i; // old value is returned
          }

          $i = 5;
          $i = ipp($i);
          echo $i;

          function ppi(&$i){ // equivalent of ++$i
          $i=$i+1; // value is altered
          return $i; // new value is returned
          }

          $j = 10;
          $j = ppi($j);
          echo $j;

          ?>

          --
          Rami.Elomaa@gma il.com

          "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
          usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

          Comment

          • Steve

            #20
            Re: $i = $i++

            | thanks all. that's how i explained it. i didn't quite get why the value
            of
            | $i remained the same (whatever it's initial value was)...until:
            | >
            | $i is incremented. Then $i is set with the old (pre-increment) value of
            | $i (++ has precedence over =).
            | >
            | thanks jerry. it just snapped for me. it's that even though $i++ may
            | increment, the lhs assignment is made from $i's initial value prior to
            the
            | increment... it's the = that resets/nullifies the ++ in this case. that
            | about right?
            |
            | Yes. That's why the assignment is not used with $i++. $i++ is a
            | shorthand that both increments and assigns at the same time. If you go
            | and add another assignment then it's fucked.
            |
            | $i = ++$i; // this would've worked as expected, though.
            |
            | Maybe this helps:

            no, no...i got it. not only did i explain it correctly in alt.php but gave
            examples - even explaining that $i = ++$i *would* give expected results. i
            was just trying to do it all in my head and match it up with each example's
            results. i'm there now.

            thx,

            me


            Comment

            • Malcolm Dew-Jones

              #21
              Re: $i = $i++

              David T. Ashley (dta@e3ft.com) wrote:
              : "Andy Hassall" <andy@andyh.co. ukwrote in message
              : news:63nq13tlvg 0fvp5i0lg54uvlo 18nhi5n3f@4ax.c om...
              : 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/seqpoints.html
              : http://c-faq.com/expr/ieqiplusplus.html

              : I've never really understood why

              : $i = $i++;

              : _needs_ to be undefined in C, C++, or PHP. It seems that in order to assign
              : the left side, one needs to evaluate the right side,

              No, in order to assign the left side you only need to evaluate that part
              of the right side that provides the original (pre-incremented) value.
              The increment can happen any time afterwards as long as it's before the
              next occasion when the variable is used. In fact if the variable was not
              used later then the compiler could skip the increment altogether.

              The language definition of C does not restrict the ordering so that
              compilers are free to use as many techniques as possible to optimize the
              code. Operations such as an increment can be run in parallel with the
              rest of a statement (on hardware that has that sort of capability), but
              only if the value does not need to be available within the statement
              itself.

              However, I don't write compilers so what I say is just my interpretation
              of other things I've read.

              Comment

              • Gordon Burditt

                #22
                Re: $i = $i++

                >I've never really understood why
                >
                $i = $i++;
                >
                >_needs_ to be undefined in C, C++, or PHP.
                It's difficult to find these things at compile time, or it would
                have been defined as a fatal error at compile time. $i = $i++ is
                a simple and obvious case of a much more complicated problem.
                Preferably a standard would also demand deletion of all copies of
                the source code the compiler could find.
                >It seems that in order to assign
                >the left side, one needs to evaluate the right side, and since ++ has to
                >bind tighter than = ... it seems there should be an unequivocal "right"
                >answer ... $i will be unchanged.
                Which binds tighter, the increment of $i or returning the value of the
                previous value? That's two separate operations represented by ONE
                operator, ++.

                Comment

                Working...