In PHP to check non-equality (without checking type) you can do this:
if( A != B ) {
DO SOMETHING;
}
But you can also do this, which has the same result:
if( A <> B ) {
DO SOMETHING;
}
Is there any difference?
Does using !=
over <>
change the evaluation in any way, shape, or form?
is a binary operator that returns true if its two arguments are not equal to each other. NOT is a unary operator, which reverses its argument, a Boolean expression. For example, this expression: a < 10 is true when a is any value less than 10. This condition can be negated: NOT a < 10 .
!= will only check value regardless of operands type. but !== is used to compare both value & type of 2 operands that are being compared to each other.
These are defined to be identical. As you can see, the -> basically just combines a * and a . into a single operation. If you were dealing directly with an object or a reference to an object, you'd be able to use the . without dereferencing a pointer first:
Note that :: should be used with a class name rather than a class instance, since static fields or methods are common to all instances of a class. Show activity on this post. Put very simple :: is the scoping operator, . is the access operator (I forget what the actual name is?), and -> is the dereference arrow.
The . and -> operators are for accessing an object instance's members, and only comes into play after creating an object instance. You use . if you have an actual object (or a reference to the object, declared with & in the declared type), and you use -> if you have a pointer to an object (declared with * in the declared type).
There is no difference at runtime. The only difference between the two types of quotes is the one you have already pointed out: Single quotes need to be escaped inside single quoted string literals but not inside double-quoted string literals.
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...
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
No difference.
However, !=
allows the convenience of more easily adding an extra =
to force type comparison.
One's old, one's new.
according to the manual:
$a != $b Not equal TRUE if $a is not equal to $b after type juggling.
$a <> $b Not equal TRUE if $a is not equal to $b after type juggling.
use !=
.
They have the same order of precedence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With