Web Analytics Made Easy -
StatCounter x++ vs. x+1 - CodingForum

Announcement

Collapse
No announcement yet.

x++ vs. x+1

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • x++ vs. x+1

    Is there a difference between x++ and x+1? Or are they the same? Which one should I use?

    Thanks in advance.

    Regards,
    "DLA"
    Always Use a DOCTYPE!
    Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
    Always Use only CSS for layout; never (X)HTML.
    Always Have fun while coding!

  • #2
    $x++ is the equivalent of $x = $x + 1; (for numbers at least) but you should always use $x++ since it's cleaner and a bit faster (in C++, but not sure for PHP). Also when possible, I use ++$x; (avod 1 copy of $x in memory in C++, but not sure for PHP).

    Basically
    $x++;
    ++$x;
    $x = $x + 1;
    are all the same (in this context), but I prefer by far the first two ways (depending of the situation).

    ++ before or after the variable will be different for things like for :

    for ($i = 0; $i < 10; $i++) //$i starts at 0
    !!! is not the same as !!!
    for ($i = 0; $i < 10; ++$i) //$i starts at 1
    Last edited by AlexV; Apr 7, 2009, 03:29 PM.

    Comment


    • #3
      Thanks
      Always Use a DOCTYPE!
      Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
      Always Use only CSS for layout; never (X)HTML.
      Always Have fun while coding!

      Comment


      • #4
        Originally posted by TheWolf1 View Post
        for ($i = 0; $i < 10; $i++) //$i starts at 0
        !!! is not the same as !!!
        for ($i = 0; $i < 10; ++$i) //$i starts at 1
        not true, both start at 0. it's not a good example,
        .
        this is a stupid example of post/pre increment:
        PHP Code:
        $i 0;
        print 
        $i "\n";
        print 
        $i++ . "\n";
        print 
        $i "\n"
        will print:
        Code:
        0
        0
        1
        and:
        PHP Code:
        $i 0;
        print 
        $i "\n";
        print ++
        $i "\n";
        print 
        $i "\n"
        will print:
        Code:
        0
        1
        1
        best regards

        Comment


        • #5
          The for example is like Oesxyl says the same. The last parameter in the for is the post change.
          PHP follows C. So, when you don't need to capture the value of the variable during incrementation, use a pre increment operator. You should be able to test this on a function stack with:
          PHP Code:
          function increment(&$x)
          {
              
          $x++;
          }

          $x 1;
          increment($x);
          print 
          $x// 1 
          This is because to do a post-increment under the hood it will create an additional variable and perform the assignment back to $x. This differs in the pre-increment in that the pre-increment does not require an additional variable. The only time this will be an issue is when the reference is being adjusted out of the original scope.
          PHP Code:
          header('HTTP/1.1 420 Enhance Your Calm'); 
          Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

          Comment


          • #6
            Originally posted by oesxyl View Post
            print ++$i . "\n";
            Can you think of a situation where you might need this? I mean a time where you couldn't just increment before hand.
            You can not say you know how to do something, until you can teach it to someone else.

            Comment


            • #7
              Originally posted by Fou-Lu View Post
              function increment(&$x)
              {
              $x++;
              }

              $x = 1;
              increment($x);
              print $x; // 1
              That prints 2 for me. So does.

              PHP Code:
              function increment(&$x)
              {
                  ++
              $x;
              }

              $x 1;
              increment($x);
              print 
              $x// 2 
              You can not say you know how to do something, until you can teach it to someone else.

              Comment


              • #8
                Originally posted by timgolding View Post
                Can you think of a situation where you might need this? I mean a time where you couldn't just increment before hand.
                Technically, you should only ever use a pre-increment to save on memory. In PHP this equates to faster run times, though with modern processing there is no need to concern ourselves with such a little amount of time and memory.
                But, if you need to capture the value of $x after an increment to print it, you'd be better off doing:
                PHP Code:
                $x 1;
                ++
                $x;
                print (
                $x 1); 
                Instead of using a print $x++;. This is such a minor thing though, and it really comes down to how the referencing is making use of the variable.

                Edit:
                I see you snuck one in there
                If the code produces a 2 for the $x++, it means its not the same as how C handles it. I would have expected it to destroy the temporary variable on the stack, but that is my mistake then. The ++$x will always return the incremented value though, since it doesn't need to create memory on the stack.
                Last edited by Fou-Lu; Apr 7, 2009, 08:12 PM.
                PHP Code:
                header('HTTP/1.1 420 Enhance Your Calm'); 
                Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                Comment


                • #9
                  ah ok thanks for the explination You can have a thanks
                  You can not say you know how to do something, until you can teach it to someone else.

                  Comment


                  • #10
                    Originally posted by timgolding View Post
                    Can you think of a situation where you might need this? I mean a time where you couldn't just increment before hand.
                    yes, you can show the effect of the operator in a thread on coding forum,
                    as I said, the example is stupid,

                    best regards

                    Comment


                    • #11
                      Originally posted by oesxyl View Post
                      yes, you can show the effect of the operator in a thread on coding forum,
                      as I said, the example is stupid,

                      best regards

                      haha
                      You can not say you know how to do something, until you can teach it to someone else.

                      Comment


                      • #12
                        Originally posted by timgolding View Post
                        Can you think of a situation where you might need this? I mean a time where you couldn't just increment before hand.
                        There are reasonable situations for both pre and post incrementing. Say I want to use the value of $i before I increment it:

                        PHP Code:
                        $i 0;
                        for (
                        $n 4$n 10$n++) {
                          
                        $meaningOfLife $n/(2*++$i);

                        But maybe for my needs I want to use the value of $i after it's been incremented:

                        PHP Code:
                        $i 0;
                        for (
                        $n 4$n 10$n++) {
                          
                        $meaningOfLife $n/(2*$i++);

                        Both are reasonable, just depends on what I need to get done.

                        Comment


                        • #13
                          hah? can someone again review for me what is the BASIC difference and SAMPLE of pre and post incremenet. i learnt it but kinda forgot because i barely use pre increment thanks ! hehehe ^^ :P

                          Comment


                          • #14
                            Post:
                            PHP Code:
                            $x 0;
                            print 
                            $x++;  // Prints 0
                            print $x// Prints 1 
                            Pre:
                            PHP Code:
                            $x 0;
                            print ++
                            $x// Prints 1
                            print $x// Prints 1 

                            Simply put, if you create an assignment during the incrementing, the value of a pre-increment will be that of the current variable plus one, while using a post operator returns the value of the current variable, and then adds one.
                            PHP Code:
                            header('HTTP/1.1 420 Enhance Your Calm'); 
                            Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

                            Comment


                            • #15
                              got you thanks!

                              Comment

                              Working...
                              X