Search code examples
phpif-statementzero

comparing strings with -0 fail


I cannot manage to do a succesful comparison with the string "-0"

if(strval("-0") == strval("0")) {
    echo '"-0" seems to be same "0"';
}

Result:

"-0" seems to be same "0"

What am I missing here?

And more important, how to work around this?


Solution

  • Use the "===" operator

    if(strval("-0") === strval("0")) {
        echo '"-0" seems to be same "0"';
    }
    

    but why are you using strval() if the values are strings already?