Search code examples
phpcomparisonequals-operator

Are all PHP equality comparisons symmetric?


Is $a == $b always equivalent to $b == $a?

I think in JavaScript there are a few weird cases where that's not true, due to casting.

I think ide is correct. I'll ask another question.


Solution

  • Depends what happens between those two calls. Otherwise yes, those are the same. The order makes no difference. Using 2 equals == A string of 1 and integer of 1, will return true when compared. Type is ignored, only value is compared. So no wierdness.

    http://php.net/manual/en/language.operators.comparison.php

    <?
    
    $a=(string) 1;
    $b=(int) 1;
    
    var_dump($a);
    var_dump($b);
    
    
    echo $a==$b;
    

    Outputs: 1

    http://www.ideone.com/JLJWQ

    EDIT

    To clarify, there is absolutely nothing you can ever put in $a or $b to get a different output on the comparison, just by putting it on the other side of the operator.

    $a="1234";
    $b="1234";
    
    echo $a==$b;
    echo $b==$a;
    

    The output of that, for any $a or $b values, will always without a doubt be true true, or false false.