Problem
When I was modifying PHP code written by someone else the other day, I came across this. I couldn’t understand why a simple comparison (if ($var ==! ” “) didn’t work. After some testing, I discovered that the code’s author used the comparison operator ==! instead of!==. I’d never encountered ==! before in any language, so I did some testing to see if it could even work:
<?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 results in the following 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)
For boolean and integer variables, the operator appears to operate, but not for strings. I can’t find anything about ==! in the PHP docs or on any search engine (tried Google, Bing, DuckDuckGo, but I suspect they try to interpret it instead of searching for the literal string). Is there anyone who has seen this before and can shed some light on it?
Asked by Gerald Schneider
Solution #1
The only difference is that there isn’t a ==! operator.
This expression:
$a ==! $b
Is essentially the same as:
$a == (!$b)
Answered by Bang Dao
Solution #2
It’s merely a mash-up of == and!. The only meaningful operator in this case is ==. As a result, the combination ==! will work just like a regular ==, testing equality, and believe me when I say that it works.
$variable_a ==! $variable_b
is nobody else but
$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 as an operator may not always work. If we take the operators = and!, for example, they will only work as operators if they are in the pattern of!= or!==. These characters can be combined in a variety of ways, such as!====,!==!, and so on. Operator combinations should be in a unique format, sequence, and combinations (all characters should not be combined with each other), and there should be no space between them.
Check out the list of operators below;
Answered by Alfred
Solution #3
==! is not a single operator, but rather two:
== and !
! being more important than ==
So :
“a”!== ” “: bool(true) –> true since “a” isn’t the same as ” “.
“a” ==! ” “: bool(false) –> false because “a” does not equal!
It’s possible to put a space between == and!.
Answered by Michael Laffargue
Solution #4
There is no such thing as ==! It’s a really obscure ==! notation.
You might just as easily write a –> b, which evaluates to a— > b, but it will appear funny because spaces don’t matter in those operations.
So, in response to the inquiry, “a” ==! ” will be parsed as “a” ==!” Casting covers the negation of a string, therefore any string other than “0” and ” ” is true when casted.
As a result, the expression “a” ==!” ” is transferred:
And, because string “a” is not the same as bool true, the entire statement evaluates to false.
So, what is the story’s moral? Don’t be fooled by missing or incorrectly positioned spaces! 🙂
Answered by F.P
Solution #5
==! 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)
}
Answered by AD7six
Post is based on https://stackoverflow.com/questions/12313423/difference-between-and