163

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?

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • 2
    similar to http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator – Carlos Campderrós Sep 07 '12 at 07:47
  • even more similar: [What is the difference between != and =! in java?](http://stackoverflow.com/questions/8825840/what-is-the-difference-between-and-in-java) – Mysticial Sep 07 '12 at 13:31
  • 58
    come on... what is with the upvotes? I can make a dozen more questions like this up. And accompany them by quasi serious analysis. This is standard textbook exercise practice, no. In SO, we vote for quality and information, not for fun and entertainment. – sehe Sep 07 '12 at 14:12
  • 1
    PS. I'm ***not*** saying the OP made this up (on purpose). I ***am*** saying SO is not achieving better content with voting patterns like this – sehe Sep 07 '12 at 14:13
  • This goes to show that a language-fixed amount of operators with greedy parsing can be just as problematic as the oh-so-"dangerous" allowing of custom-defined operators. In e.g. Haskell, the equivalent (misspelling the inequality `/=` as `=/`) would fail with a simple and concise error message: `Not in scope: '=/'` `Perhaps you meant one of these:` `'/' (imported from Prelude), '/=' (imported from Prelude),` `'==' (imported from Prelude)` — Though, thinking about it, the problem actually has more to do with having both infix and prefix operators. – leftaroundabout Sep 07 '12 at 14:23
  • 51
    @sehe He's getting upvotes because its the definition of a well researched question - he has "thouroughly searched for an answer", "been specific", the question is "relevant to others", and is "on topic". the OP has tried things and shown us what he's tried; its the kind of question this site tries to foster – JRaymond Sep 07 '12 at 15:59
  • 12
    @JRaymond: he's getting upvotes because it's a trivia question for a very easy to understand problem and answer. The trivia aspect increases curiosity which drives more people in, and the easy aspect increases the percentage of those people who understand the question and the answer enough to vote on it. It is not related to it being a good question or not. – Andreas Bonini Sep 07 '12 at 16:16
  • 2
    @AndreasBonini I would argue that they come for the trivia, they upvote for the quality of the question as posed - Lestways that's what I did, far be it from me to denigrate the motives of others. At the least he doesn't deserve the downvotes. – JRaymond Sep 07 '12 at 16:29
  • 4
    @JRaymond, admit it, most of the high up-vote questions _are_ trivia. Take a look at these three questions of mine among many others: [1](http://stackoverflow.com/q/11034689/912144), [2](http://stackoverflow.com/q/9831567/912144), [3](http://stackoverflow.com/q/7713459/912144). None of them are something you could just find on google (unlike most high up-vote questions) and they are well-thought and tried before being written (I know, because I wrote them). However, since they are questions that require expertise, they never get high votes. That's just how the system here works. – Shahbaz Sep 07 '12 at 16:54
  • 3
    @JRaymond sehe is right. Writers would have a field-day on SO. I'd suggest going down this list (http://php.net/quickref.php) and coming up with interesting problems where the function is the solution. The more obscure and interesting the situation or function the more votes. – Mike B Sep 07 '12 at 23:20
  • I've read somewhere that ==! processes slightly faster then !==. Nothing exponential, but really just comes down to the programmer's personal preference. – ShayneStatzell Sep 04 '13 at 00:29
  • @MysteryMeatMonster have you actually read the answers here? – Gerald Schneider Sep 04 '13 at 05:27

5 Answers5

244

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

This expression:

$a ==! $b

Is basically the same as this:

$a == (!$b)
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Bang Dao
  • 5,091
  • 1
  • 24
  • 33
  • 10
    Wow, a lot good answers in such a short time. I picked the first answer that clarified that the ! applies to the right hand variable and therefore has the most value. Sorry for the other guys ;) – Gerald Schneider Sep 07 '12 at 07:26
  • 4
    @Gerald Schneider: The least you could do to make up for it was to improve this answer so that it was at least on par with the other answers in terms of grammar and formatting (one of which you specifically called out for its very detailed explanation!). But no matter, I've edited it now. – BoltClock Sep 07 '12 at 14:28
  • I don't even program in PHP and even I realized that "==!" would be read as the 2 operators "==" and "!". It just shows how much effect a little bit of layout can have on how you read something. – StarNamer Sep 07 '12 at 17:47
  • maybe because that you does not program in PHP so that you can say like this. PHP has operator `===` and `!==` which were not appeared on any other languages (as far as I know). – Bang Dao Sep 07 '12 at 19:01
  • 2
    @Bang Dao Um, JavaScript? It has === and !==. But no ==! – psr Sep 07 '12 at 23:02
  • @psr http://www.w3schools.com/js/js_operators.asp -> I didn't see operator `===` or `!==` was listed here. – Bang Dao Sep 08 '12 at 02:29
  • 2
    @BangDao Now you found another reason for [w3fools](http://w3fools.com/) Please read it, it is important. – some Sep 08 '12 at 09:13
77

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

Alfred
  • 21,058
  • 61
  • 167
  • 249
39

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

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
29

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

F.P
  • 17,421
  • 34
  • 123
  • 189
  • 3
    unlike stated in this answer, "a" == true evaluates to true because a not empty string is casted to the boolean value true. The expression "a" === true would evaluate to false. – Pascal Rosin Sep 07 '12 at 14:42
25

==! 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)
}
Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123