Increment/Decrement Opperator
Posted: Mon Jan 09, 2012 10:27 pm
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.
The results of the three echos are
3
3
1
My question is why does this compile to 3?
When this compiles to 1, and all I am doing is adding another $var2 +?
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.
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 />";
?>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++;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.