Page 1 of 1

Increment/Decrement Opperator

Posted: Mon Jan 09, 2012 10:27 pm
by PsychoStar
So I am new to PHP but not new to programming. I know both Java and C. C and Java handle Increment and decrement operators differently, and I know this during my previous semester in one of my CS classes. The teacher and I got into it me saying the examples compiled to the same numbers, with him losing in the end because he knew Java better, and most of my knowledge coming from C. Here is the exact same C, just in PHP, and it compiles differentlly than either C or Java.

Code: Select all

        <?php 
            $var1 = 1;
            $var2 = 1;
        ?>
        
        
        <?php
            $var11 = $var1++ + $var1;
            echo $var11;
            echo "<br />";
            
            $var22 = $var2 + $var2++;
            echo $var22;
        ?>
        
        <br />
        
        <?php 
            $var3 = 1;
            $var4 = 1;
        ?>
        
        <?php
        
            $var33 = $var3++;
            echo $var33;
            echo "<br />";
        ?>
The results of the three echos are

3
3
1

My question is why does this compile to 3?

Code: Select all

 $var22 = $var2 + $var2++;


When this compiles to 1, and all I am doing is adding another $var2 +?

Code: Select all

$var33 = $var3++;
Thanks guys

P.S. And more of a QQ, starting out with C, $var11 and $var22 should both compile to 2 because I am using a post-increment operator and should do the increment after all expressions. C does it after all expressions, whereas Java does it as the expression evaluates (from left to right). If the compiler was thinking I meant $var44 = $var4 + ++$var4, this should compile to 4, not 3. I understand this is not the languages fault but the problem with learning more than one language. And I have tried to search Google with no luck.

Re: Increment/Decrement Opperator

Posted: Tue Jan 10, 2012 12:21 am
by twinedev
See the list of Order of Operation:
http://www.php.net/manual/en/language.o ... edence.php, ++ and -- are the second on the list of what to do.
http://www.php.net/manual/en/language.o ... rement.php says that it $var first returns the value of the variable then increases it

The issues is that PHP seems to assign values from variables AS they are used (in the order it evaluates, first link above), not "replace all variables and solve".

Code: Select all

$var22  =  $var2  +  $var2++  ;
$var22  =  $var2  +    1      ;  // #1, Return to equation value of $var2
($var2 = $var2 + 1;)             // #2 now increase $var2 by one
$var22  =    2    +    1      ;  // #3 return to equation value of $var2  (now 2)
$var22  =         3           ;  //   #4 add the numbers
   3                          ;    // #5 assign to $var22;    
Thanks for bringing it up, never thought about it before. Made me do some testing on the behavior :-)

-Greg

Re: Increment/Decrement Opperator

Posted: Tue Jan 10, 2012 11:48 am
by PsychoStar
Thank you for your reply. Your insightfulness is exactly what I was looking for.

Guess just another variation I have to learn :-P

Thanks once again.