1
$a = 3;  
$b = $a++;

if ($a > $b) { echo “a > $b” }
else if ($a == $b) { echo “a = $b” }
else { echo “a < $b” }

When I work through this question, I get a=3, b=4 (3+1). Hence both the If and Else If conditions are false, so I go to Else and final answer is: a < 4.

However, the answer according to the mark scheme is: a > 3 meaning that the If condition is true. How could $a be larger than $b? Thanks

Mureinik
  • 297,002
  • 52
  • 306
  • 350

1 Answers1

6

Take a look at the following statement:

$b = $a++;

The ++ is located after $a. This is the post-increment operator. It first returns the current value of $a (3), and only then increments $a. In other words, $b is assigned with $a's current value, and then $a is incremented. So, $a is 4 and $b is 3, hence $a > $b.

Mureinik
  • 297,002
  • 52
  • 306
  • 350