views:

160

answers:

4

I just happened to stumbled upon a piece of php code and could see author used <> to do a not equal to comparison:

if ($variable <> "") {
    echo "Hello, I am having some value";
}

I have always used !=:

if ($variable != "") {
    echo "Hello, I am having some value";
}

Are there any special circumstances, when I should use <> over !=?

+3  A: 

I believe when PHP was first developed, one of the design goals was to make the language flexible, which is why they brought in every loop type and every operator type you could possibly think of.

<> is slightly different than != in terms of precedence category, but the operators that come between them means that there is no practical difference whatsoever.

zombat
+2  A: 

Good question. They are the same, apart from one subtle difference: <> has higher precedence than !=

Why have them? Just to make things work how you might expect. PHP is a mish-mash of borrowed ideas, and where a C programmer might prefer $foo != $bar, someone with a BASIC background might find $foo <> $bar easier on the eye. Each to their own!

Paul Dixon
ah I see .. at least there is "some" difference. ;-)
Wbdvlpr
+1  A: 

They have very close, but slightly different precedences, but I can't see any time that would be useful.

David Dorward
just was curious
Wbdvlpr
+2  A: 

I guess the <> operator has been implemented in PHP because it is present in some other languages (SQL, for instance)

Both <> and != mean almost exactly the same : the only difference I've managed to find is related to their precedence : see Operator Precedence : they are not on the same line ^^ (which means there's a difference, afterall, between those two)

Though, I have to admit I have never seen the <> used in PHP.

Note that people generally use != ; you should probably do the same : it will make your code easier to understand.

And, btw, you also have the !== operator, which does type comparison too ; but there is no <<>> operator or anything like that ^^


As a reference : Comparison Operators

Pascal MARTIN
I agree with you regarding making the code easier to understand.
Wbdvlpr
Actually, I have ton confess : I didnt' even remember there was a <> operator in PHP ^^ I even tested your code to make sure it worked ^^ Seing it in some source-code would definitly feel strange ! (I suppose I had seen it in the manual a long time ago... But didn't even remember ^^ )
Pascal MARTIN