DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Stupid question ... what is the difference between != and <>?  (Read 10,430 times)

barney

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,294
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.
Make a good day ... barn

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Stupid question ... what is the difference between != and <>?
« Reply #1 on: August 13, 2011, 08:31 PM »
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

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,294
Re: Stupid question ... what is the difference between != and <>?
« Reply #2 on: August 14, 2011, 01:17 AM »
'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.
Make a good day ... barn

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Stupid question ... what is the difference between != and <>?
« Reply #3 on: August 16, 2011, 03:59 PM »
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.
- carpe noctem

kamahl

  • Supporting Member
  • Joined in 2010
  • **
  • Posts: 48
    • Participated in NANY Challenge Twice
Re: Stupid question ... what is the difference between != and <>?
« Reply #4 on: August 22, 2011, 06:23 AM »
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,
Code
 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. 

anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 802
    • Participated in NANY Challenge Thirteen Times
Re: Stupid question ... what is the difference between != and <>?
« Reply #5 on: August 22, 2011, 05:26 PM »
Hi barney

There _IS_ a difference between !$var==[somevalue] and $var<>[somevalue].

In '<>' the characters are compared up to the length of the right character string, e.g.

"A"  <> "Z"      // result: TRUE
"A"  <> "A "     // result: TRUE
"A " <> "A"      // result: FALSE (means they are equal)
""   <> "A"      // result: TRUE

'==' returns TRUE only when both character strings contain exactly the same characters, so a ! (NOT) before it will return TRUE when not exactly same e.g.

! "A"   == "A"                    // result: FALSE
! "A"   == "A "                   // result: TRUE
! "A "  == "A"                    // result: TRUE
! ""    == "A"                    // result: TRUE
! "AB"  == "ABC"                  // result: TRUE
! "ABC" == "ABC"                  // result: FALSE

In short use '<>' if you do not want exact comparison, but just left most ones, e.g.
MPT <> "P"   // where MPT has "P1" value, so this will return FALSE, and similarly,
! LEFT(MPT,1) == "P" // where MPT has "P1" value, so this will return FALSE

Hope I was able to help you a bit in fixing your old codes :)

Regards,

Anand

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Stupid question ... what is the difference between != and <>?
« Reply #6 on: August 22, 2011, 07:48 PM »
There _IS_ a difference between !$var==[somevalue] and $var<>[somevalue].

In '<>' the characters are compared up to the length of the right character string, e.g.
Hm, if that's true then there's either a bug in the PHP documentation (pretty likely) or a bug in the implementation (almost even more likely).
- carpe noctem

barney

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,294
Re: Stupid question ... what is the difference between != and <>?
« Reply #7 on: August 22, 2011, 08:38 PM »
Hm, if that's true then there's either a bug in the PHP documentation (pretty likely) or a bug in the implementation (almost even more likely).
Likely, the man says  :P.

Make that a certainty, methinks  :P.

I'd say the PHP docs are better than most - Apache docs make me wanna scream - but that's not saying much.  Did a stint as a tech writer in the corporate world.  If I'd submitted anything even close to most of the documentation I see in the open source arena - whether free or not - I'd have been canned on the spot!  And the current crop of docs for shareware/payware is pretty much on the same playing field.  Can we say, "Least common denominator?!?"

Anyway, I'm accustomed to the <> operator as opposed to the != operator, so that's what I'm using as I rewrite parts of this code.  It's not commented, so I'm doing a lot by intuition - and commenting to a point where Hell wouldn't have it  :o.  At least, the next guy won't wonder why I did something, although he may have serious questions along the lines of, "Why he do that"? ;D
Make a good day ... barn

Original address: https://www.donationcoder.com/forum/index.php?topic=27631.0