Website Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • August 30, 2015, 09:34:17 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

barney

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,269
    • View Profile
    • Read more about this member.
    • Donate to Member
Stupid question ... what is the difference between != and <>?
« on: August 13, 2011, 03:24:21 PM »
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

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 34,932
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #1 on: August 13, 2011, 03:31:02 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/m...ators.comparison.php
and here:
http://php.net/manua...ypes.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,269
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #2 on: August 13, 2011, 08:17:55 PM »
'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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 8,858
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #3 on: August 16, 2011, 10:59:00 AM »
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
  • **
  • gravatar avatar
  • Posts: 48
    • View Profile
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #4 on: August 22, 2011, 01:23:34 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: [Select]
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: 304
    • View Profile
    • Apps, to help you
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #5 on: August 22, 2011, 12:26:05 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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 8,858
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #6 on: August 22, 2011, 02:48:28 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,269
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Stupid question ... what is the difference between != and <>?
« Reply #7 on: August 22, 2011, 03:38:32 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