16
What’s the difference between !=
and <>
? Which one should I wear?
8
According to the PHP documentation both are comparison operators and serve to compare whether one value is different from another.
Example (using the function var_dump()
to return the result):
<?php
var_dump( 7 != 7 ); // FALSE, pois não são diferentes
var_dump( 7 != 6 ); // TRUE, pois são diferentes
var_dump( 7 <> 7 ); // FALSE, pois não são diferentes
var_dump( 7 <> 6 ); // TRUE, pois são diferentes
?>
The use varies according to your preference, but the most common is to use !=
.
You can see more about directly in the PHP documentation here and on the precedence here.
7
None, they are exact synonyms. They are just different ways to write. Choose which one to find more comfortable. I see that it is more common to use the !=
to be symmetrical with the ==
("equal"), since !=
would be the same as saying "not equal".
Browser other questions tagged php operators
You are not signed in. Login or sign up in order to post.
n-e-n-h-u-m-a, it’s a filler php
– CypherPotato
@RZ-8121 It’s not a matter of filler and yes question that at the time php was created (1995) there were languages like VB (classico) that used
<>
and other languages they used!=
or the idea was to facilitate migration. People actually criticize languages to pie and right, but many languages were born at different times of today’s thinking. Do not misunderstand me, just consider it as a constructive criticism. That is to say it is only to maintain the "portability".– Guilherme Nascimento
Two operators for the same function is somewhat confusing. It can be useful even for some who like more than
<>
than the!=
, but it’s like the bigown said, it’s a language that the equality operator is the==
, then the correct would be for the programmer to use the!=
to deny an equality, it would make no sense to use the<>
and the opposite is the==
...– CypherPotato
@RZ-8121 is explained here http://answall.com/questions/47887/qual-a-difference-entre-e-em-php-qual-use#comment234361_47899 :)
– Guilherme Nascimento
@RZ-8121 is it not clear to you that operators do the same thing? Where is the precedence? "Different from" is "different from" and ready. Putting information that is untrue in a response (the manual says nothing or says otherwise) is a mistake.
– Wallace Maxters
@RZ-8121 in programming languages there is sometimes more than one way to do the same thing and it is not just a php feature. Every language has its peculiarities.
– Guilherme Nascimento