-2
 <?php

 $x=11;
if ($x++>11)
{
    echo "$x";
}
else
{
    echo "not greater than $x";
}
?>

Output of this code is - not greater than 12

I want to know why this happens. Thanks!

2 Answers2

1

Due to Precedence and Increment. The value is compared before it is incremented. Therefore, that condition is false. If you do ++$x instead of x$++, then you will have a different result due to the pre and post increment. If you put brackets around $x++ then it will be evaluated first and you will have it evaluated to true.

Ice76
  • 1,143
  • 8
  • 16
1

The issue here is that there are two different incrementing operators. See the documentation.

Basically:

  • $x++ uses $x as-is, then increments.
  • ++$x increments, then uses the variable.
Dan
  • 101
  • 4