4

Might be a duplicate, but I never found an answer. I ran some basic tests with incrementing operator:

   $i = 42;
   $i > ++$i; # false
   $i > $i++; # true

If the first comparison makes sense to me returning false, I can not figure out why the second returns true.

Even if it is pre or post incrementation, I would say both are false. How my variable can be strictly superior than its own incremented value?

Same tests with C and C++ show warnings but say false to both comparisons.

  • 7
    You pre-incremented `$i` to 43 in the previous command, thus the expression is evaluated as `43 > 43`, which naturally gives false. What I believe happens next is: 1) PHP has an expression to calculate on the right side, which is post-decrement, meaning `$i` is first returned as 43 and then increased to 44; 2) `$i` is now 44 and is thus interpreted as such, yielding the expression of `44 > 43`. I'm not good with PHP internals, but that's the only scenario I can imagine. Maybe someone more knowledgeable can shed some light on it. – El_Vanja May 16 '21 at 21:03
  • You say the C and C++ compilers you tried gave warnings; were they warning of undefined behaviour? Because that's what this is in PHP: it's not that one expression always gives false and the other always true, it's that both may change arbitrarily between versions or optimisations, because the language doesn't guarantee the order of operations. The linked duplicate explains this in more detail. – IMSoP May 16 '21 at 22:28

0 Answers0