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";

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";
```

ASKER CERTIFIED SOLUTION

membership

This solution is only available to members.

To access this solution, you must be a member of Experts Exchange.

ASKER