<?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!
<?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!
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.
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.