php - Difference between "!==" and "==!"


Translate

Yesterday I stumbled over this when I modified PHP code written by someone else. I was baffled that a simple comparison (if ($var ==! " ")) didn't work as expected. After some testing I realized that whoever wrote that code used ==! instead of !== as comparison operator. I've never seen ==! in any language so I wondered how the hell this code could even work and did some testing:

<?php
echo "int\n";
echo "1 !== 0: "; var_dump(1 !== 0);
echo "1 !== 1: "; var_dump(1 !== 1);
echo "1 ==! 0: "; var_dump(1 ==! 0);
echo "1 ==! 1: "; var_dump(1 ==! 1);
echo "bool\n";
echo "true !== false: "; var_dump(true !== false);
echo "true !== true: "; var_dump(true !== true);
echo "true ==! false: "; var_dump(true ==! false);
echo "true ==! true: "; var_dump(true ==! true);
echo "string\n";
echo '"a" !== " ": '; var_dump("a" !== " ");
echo '"a" !== "a": '; var_dump("a" !== "a");
echo '"a" ==! " ": '; var_dump("a" ==! " ");
echo '"a" ==! "a": '; var_dump("a" ==! "a");
?>

This produces this output:

int
1 !== 0: bool(true)
1 !== 1: bool(false)
1 ==! 0: bool(true)
1 ==! 1: bool(false)
bool
true !== false: bool(true)
true !== true: bool(false)
true ==! false: bool(true)
true ==! true: bool(false)
string
"a" !== " ": bool(true)
"a" !== "a": bool(false)
"a" ==! " ": bool(false)
"a" ==! "a": bool(false)

The operator seems to work for boolean and integer variables, but not for strings. I can't find ==! in the PHP documentation or anything about it on any search engine (tried Google, Bing, DuckDuckGo, but I suspect they try to interpret it instead of searching for the literal string). Has anybody seen this before and can shed any light on this behavior?


All Answers
  • Translate

    The difference is that there is no operator ==!.

    This expression:

    $a ==! $b
    

    Is basically the same as this:

    $a == (!$b)
    

  • Translate

    There is no ==! operator in PHP

    Its just a combination of == and !. Only relevant operator present here is ==. So the combination ==! will work just as a normal ==, checking Equality, and trust me,

    $variable_a ==! $variable_b 
    

    is none other than

    $variable_a == (!$variable_b)
    

    and thus;

    "a" ==! " ": bool(false)
    "a" ==! "a": bool(false) //is same as "a" == (!"a")
    

    And

    true ==! false: bool(true)
    true ==! true: bool(false)
    

    Combining multiple operators characters may not work as an operator always. for example, if we take = and !, it will work as operators only if it is in the pattern of != or !==. There can be numerous combinations for these characters like !====, !==! etc.. etc.. Operator combinations should be in unique format, unique order, unique combinations (all characters wont combine with all other characters) and definitely, without any space between them.

    Check the operators list below;

    enter image description here


  • Translate

    ==! is not an operator but two :

    == and !

    ! having a higher priority than ==

    So :

    "a" !== " ": bool(true) --> true because "a" is really not equal to " "

    "a" ==! " ": bool(false) --> false because "a" is not equals to !" "

    Could be written with a space between == and !.


  • Translate

    ==! doesn't exist as such. It's a somewhat cryptic notation of == !

    As spaces don't matter in those operations, you could just as easily write a --> b, which evaluates to a-- > b, but will look strange.

    So, as to the question: "a" ==! " " will be parsed to "a" == !" ". Negation of a string is covered by casting, meaning any string but "0" and " " is, when casted, true.

    Thus, the expression "a" == !" " will get transferred:

    1. "a" == !" "
    2. "a" == !false
    3. "a" == true

    And, as string "a" is not the same as bool true, this evaluates the whole expression to false.

    So, what's the moral of the story? Don't let yourself be confused by missing or wrong placed spaces! :)


  • Translate

    ==! is not an operator

    ==! isn't a php comparison operator at all - it is the same as == ! (note the space)

    I.e.

    if ("a" !== " ") {
        // evaluates to true - "a" and " " are not equal
    }
    
    if ("a" == !" ") {
        // unreachable
    } else {
        // evaluates to false - "a" is not equal to true (!" " evaluates to true)
    }