Difference between “!==” and “==!” [closed]

橙三吉。 提交于 2019-12-02 14:11:10
Bang Dao

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

This expression:

$a ==! $b

Is basically the same as this:

$a == (!$b)

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;

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

==! 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! :)

==! 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)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!