What is the difference between "!=" and "" in PHP? Which one to use?

15

What is the difference between != and <> ? Which one should I use?

    
asked by anonymous 22.01.2015 / 16:12

2 answers

8

According to the PHP documentation both are comparison operators and serve to compare if one value is different from another.

Example (using the var_dump() function 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 usage varies depending on your preference, but the most common is to use != .

You can see more about directly in the PHP documentation here

strong> and about precedence here.

    
22.01.2015 / 16:29
7

None, are exact synonyms. It's just different ways of writing. Choose which one you find most comfortable. I see that it is more common to use != to be symmetric with == ("equal"), since != would be the same as say "not equal".

Documentation .

    
22.01.2015 / 16:28