Welcome Guest.   Make a donation to an author on the site July 25, 2014, 03:24:03 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Free DonationCoder.com Member Kit: Submit Request.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Stupid question ... what is the difference between != and <>?  (Read 2067 times)
barney
Charter Member
***
Posts: 1,238


see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« on: August 13, 2011, 03:24:21 PM »

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

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  undecided.  In fact, those differences may not apply to PHP - I just cannot find an adequate reference  Angry, so I'm consulting my ultimate reference:  DC smiley.

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

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.
Logged

Make a good day ... barn
mouser
First Author
Administrator
*****
Posts: 33,180



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« 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/manual....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.
Logged
barney
Charter Member
***
Posts: 1,238


see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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  thumbs 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  tongue.

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  Angry cheesy.
Logged

Make a good day ... barn
f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« 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.
Logged

- carpe noctem
kamahl
Supporting Member
**
Posts: 48


see users location on a map View Profile Give some DonationCredits to this forum member
« 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,
[copy or print]
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. 
Logged
anandcoral
Honorary Member
**
Posts: 219



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« 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 smiley

Regards,

Anand

Logged
f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« 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).
Logged

- carpe noctem
barney
Charter Member
***
Posts: 1,238


see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« 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  tongue.

Make that a certainty, methinks  tongue.

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  ohmy.  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"? Grin
Logged

Make a good day ... barn
Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.174s | Server load: 0.06 ]