Link to home
Start Free TrialLog in
Avatar of PurpleSlade
PurpleSlade

asked on

Why does $c evaluate to 4?

Ok, this is probably an amazingly stupid question but I was just playing around with preincrementing/postincrementing.   Wrote the following code:

$a = 5;
print $a . "\n";
print ++$a . "\n";
$a--;
print $a . "\n";
print $a-- . "\n";
print $a . "\n";
$c = --$a + --$a;
print $c . "\n";

Output is:
5
6
5
5
4
4

Why does $c = 4?  I was thinking it would either do $c = --$a + --$a; as 3+3 = 6 or 3+2 = 5 (I was testing to see if it would predecrement twice while evaluating this)?  

If I substitute
$c = --$a + --$a;  with  $c = --$a + 2; the result is 5 so I know that it evaluates the first --$a as 3, correct?


$a = 5;
print $a . "\n";
print ++$a . "\n";
$a--;
print $a . "\n";
print $a-- . "\n";
print $a . "\n";
$c = --$a + --$a;
print $c . "\n";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PurpleSlade
PurpleSlade

ASKER

Thanks, I suppose I should have thought that it decremented twice before evaluating them.