Difference Between "Not Equal" Operators ≪≫ and != in PHP

Difference between not equal operators and != in PHP

In the main Zend implementation there is not any difference. You can get it from the Flex description of the PHP language scanner:

<ST_IN_SCRIPTING>"!="|"<>" {
return T_IS_NOT_EQUAL;
}

Where T_IS_NOT_EQUAL is the generated token. So the Bison parser does not distinguish between <> and != tokens and treats them equally:

%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL

Not equal to != and !== in PHP

== and != do not take into account the data type of the variables you compare. So these would all return true:

'0'   == 0
false == 0
NULL == false

=== and !== do take into account the data type. That means comparing a string to a boolean will never be true because they're of different types for example. These will all return false:

'0'   === 0
false === 0
NULL === false

You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():

// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos('Foo', 'F') != false); // bool(false)
var_dump(strpos('Foo', 'F') !== false); // bool(true), it exists so false isn't returned

PHP: How does the Not-equal and Not-identical Operators work and are they faster then the Equal or Identical Operators?

Strings are defined as a structure here:

typedef struct {
char *c;
size_t len;
size_t a;
} smart_string;

The equality operator is defined here. (The following three equality operators also perform in essentially the same way, except they skip the address check as it would always be false)

static zend_always_inline zend_bool zend_string_equals(zend_string *s1, zend_string *s2)
{
return s1 == s2 || (ZSTR_LEN(s1) == ZSTR_LEN(s2) && !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)));
}

In case you don't speak C:

First, the address of each string structure is compared, if these are equal then the strings must be equal. Otherwise, further checks are made.

Second, if the addresses are not equal, then the length of each string is compared. This is just an integer equality check as the length is part of the string's structure definition. If the lengths are not equal, false is returned.

Next, the memory contents are checked for each string with memcmp. As memcmp returns 0 if the memory contents are equal, this is negated to return true.

To explicitly answer your question: PHP avoids checking every character of a string, the only case in which every character would be checked is that if every character of the string except for the last character is equal, and that the lengths of the strings are the same.

I must say: If you are really worrying about === being slower than !==, then you really shouldn't be.

Is there a difference between !== and != in PHP?

The != operator compares value, while the !== operator compares type as well.

That means this:

var_dump(5!="5"); // bool(false)
var_dump(5!=="5"); // bool(true), because "5" and 5 are of different types

What is the difference between NOT and != operators in SQL?

NOT negates the following condition so it can be used with various operators. != is the non-standard alternative for the <> operator which means "not equal".

e.g.

NOT (a LIKE 'foo%')
NOT ( (a,b) OVERLAPS (x,y) )
NOT (a BETWEEN x AND y)
NOT (a IS NULL)

Except for the overlaps operator above could also be written as:

a NOT LIKE 'foo%'
a NOT BETWEEN x AND y
a IS NOT NULL

In some situations it might be easier to understand to negate a complete expression rather then rewriting it to mean the opposite.


NOT can however be used with <> - but that wouldn't make much sense though: NOT (a <> b) is the same as a = b. Similarly you could use NOT to negate the equality operator NOT (a = b) is the same as a <> b

difference between != and !==

!= checks value

if($a != 'true')

!== checks value and type both

if($a !== 'true') 

What is the difference between and !=

Forgetting documentation for a minute, let's check out the source code. Let's start with the scanner (lexer):

<ST_IN_SCRIPTING>"!="|"<>" {
return T_IS_NOT_EQUAL;
}

So they parse to the same token. Let's check out the parser:

expr T_IS_NOT_EQUAL expr { zend_do_binary_op(ZEND_IS_NOT_EQUAL, &$$, &$1, &$3 TSRMLS_CC); }

So we know that the opcode that's fired is ZEND_IS_NOT_EQUAL...

Now, let's check out the operation:

static int ZEND_FASTCALL  ZEND_IS_NOT_EQUAL_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
USE_OPLINE

zval *result = &EX_T(opline->result.var).tmp_var;

SAVE_OPLINE();
ZVAL_BOOL(result, fast_not_equal_function(result,
opline->op1.zv,
opline->op2.zv TSRMLS_CC));

CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
}

So there's literally no difference. Since they parse to the same token, they have exactly the same precedence (so the docs are either wrong or misleading). Since they use the same executor, and there's no decision point in the opcode routine, they execute identical code.

So yes, <> and != are 100% interchangeable, and there's absolutely no technical reason to use one over the other.

With that said, there is something significant to gain by being consistent. So I'd recommend just sticking with != and being done with it...

Edit

I've updated the docs to reflect this, and fixed another issue with the precedence order (++ and -- have the same precedence as casting). Check it out on docs.php.net

php is not equal to and is not equal,equal to

== and != check equality by value, and in PHP you can compare different types in which certain values are said to be equivalent.

For example, "" == 0 evaluates to true, even though one is a string and the other an integer.

=== and !== check the type as well as the value.

So, "" === 0 will evaluate to false.


Edit: To add another example of how this "type-juggling" may catch you out, try this:

var_dump("123abc" == 123);

Gives bool(true)!

Difference between and != comparison operators?

As you've added the sqlite tag: They mean the same thing:

Note that there are two variations of the equals and not equals operators. Equals can be either = or ==. The non-equals operator can be either != or <>.

And in fact, that seems to be a common theme across languages: In languages that allow both, they mean the same thing. (Various references below.)

Some languages only allow one or the other. In Java, you use != for "not equals;" you can't use <> (it means something else and is used in a different context). In VB.Net, it's the other way around: <> is valid, and != is not.

But a lot of languages allow both:

  • T-SQL and MySQL's variant of SQL (and Oracle seems to have both and a couple more). In all three cases, they're different ways of writing the same thing. The Wikipedia page on SQL says <> is standard, but most rDBMS's also allow !=.

  • PHP supports both, and again they mean the same thing.

  • So does Python (again they're the same thing).



Related Topics



Leave a reply



Submit