ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Main Area and Open Discussion > General Software Discussion

Stupid question ... what is the difference between != and <>?

(1/2) > >>

barney:
OK, I know this is stupid, but my diminished library cannot provide me an answer  :(.

Using PHP conventions, there are two (2) ways to indicate inequality, !$var==[somevalue] and $var<>[somevalue].

I recall from my corporate days that there is a difference 'twixt the two (2), but cannot for the life of me recall it  :-\.  In fact, those differences may not apply to PHP - I just cannot find an adequate reference  >:(, so I'm consulting my ultimate reference:  DC :).

There's a time to use $var<> and a time to use !$var==, but I simply cannot recall the rules and rationales  :-[.

I've been rebuilding a box for someone, and need to rebuild some PHP code as best I can - catastrophic failure combined with inadequate - dare I say inane? - backups has produced a need to rebuild some corrupted code segments.  Trying to understand the logic of the original (?) coder is made more difficult by the use of these two (2) conventions.

mouser:
I've not seen php code using <> but in general some languages use <> while most use != to test whether two objects are not equal.  In some languages you can use them interchangeably.  In PHP code you normally use !=

And your example of "!$var==[somevalue]" is also confusing and should never be used.  You might see something like "!($var==[somevalue])" in rare cases, which makes clearer that you are testing for the case where it is NOT true that "$var==[somevalue]"

However,

There is something more complicated and subtle that you are going to have to contend with in PHP, and it trips up a lot of people.

In PHP, in addition to == and != in PHP there are comparison operators: === and !==

You can read about them here:
http://www.php.net/manual/en/language.operators.comparison.php
and here:
http://php.net/manual/en/types.comparisons.php

Basically they are used to compare whether items are identical and do not do automatic conversions/casting from strings to numbers, etc.  So == and != are more forgiving.

barney:
'Preciate the heads up anent the == and !==: never had occasion to use either, but it's nice to know  :up:, although I'm not certain this problem goes that far  :huh:.

What I'm seeing in this code is apparently interchangeable usages of != and <>.  I remember, albeit dimly, those two methods not being equivalent, although I cannot recall why.  Maybe from VB days, or ASP, maybe Delphi?  To quote a phrase from an old Sci-Fi novel, "Is not is not not is."  Dunno, maybe that is what I recall  :P.

Anyway, from what you've said, I shouldn't have to worry about that particular convention when rebuilding this script.  I'd just hate to hose a database due to inadequate knowledge or because of inappropriate usages  >:( :D.

f0dder:
Apparently, in it's typical "oh, let's confuse people" style, PHP supports both "!=" and "<>" for inequality comparisons - and from what I can tell, they're identical.

Pascal (and thus Delphi) used <> too.

kamahl:
There is one potential reason that some languages (C++ springs to mind) would have a difference between !(x==y) and x!=y

When making a class in c++ (And some derived languages), you may overload the == operator.  Doing so allows you to check if two instances are equal, even if they are not references to the same instance.  The thing you have to remember is that the C++ compiler will not automatically overload the != operator for you. As such, if the programmer of a class has forgotten to include something similar to the following,

--- --- bool MyClass::operator!=(const MyClass &other) const {
    return !(*this == other);
  }then !(x==y) will use your equality operator, while x!=y will simply check if they are the same instance.

So yes, when dealing with poorly written code, there is a difference between the two, but only in some languages, and only if the class in question wasn't implemented properly. 

Navigation

[0] Message Index

[#] Next page

Go to full version