In PHP, is there any difference between the != and <> operators?
In the manual, it states:
$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.
I guess there are no huge differences but I'm curious.
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
They are the same. However there are also !== and === operators which test for exact equality, defined by value and type.
<> means either bigger or smaller. != means not equal. They basically mean the same thing.
As everyone is saying they are identical, one from one language branch C-style/shell, one from some others including MySQL which was highly integrated in the past.
<> should be considered syntactic sugar, a synonym for != which is the proper PHP style for not-equal.
Further emphasised by the triple character identity function !==.
The operators <> and != are the same.
However, as a matter of style, I prefer to use <> when dealing with numerical variables.
That is, if:
$a is an integer
$b is an integer
instead of asking:
// if $a is not equal to $b
if ($a != $b)
I will ask:
// if $a is either less than or greater than $b
if ($a <> $b)
This is a visual hint / reminder in my code that $a and $b are definitely both intended to be numerical rather than one or both being intentionally strings.
Related
The result of:
var_dump(null != $a = 15);
var_dump($a);
is:
bool(true)
int(15)
Why is this script not triggering an error?
Since != (not equal operator) has a higher precedence than = (assignment operator), $a should be compared to null first?
The only reason I can find is that the documentation says that this is still legal: http://php.net/manual/en/language.operators.precedence.php#example-129
It seems to be an exception to what is shown in the table above.
This is not about operator precedence but about: operator precedence lists don't tell you the details (really, they never do), e.g. about the bison rules and the resulting pattern matching and stack reducing.
Let's take the statement null != $a = 15;, for simplicity without the var_dump.
This is how the parser "sees" this statement - or: sees that it is a statement.
(I hope this will be rendered with a fix-width font everywhere...)
null != $a = 15 ;
T_VARIABLE
identifier compound_variable T_LNUMBER
namespace_name reference_variable common_scalar
general_constant base_variable scalar
scalar base_variable_with_functions_calls expr_without_variable
expr_without_variable variable = expr
expr T_IS_NOT_EQUAL \______ expr_without_variable _________/
\__________________ expr ____________________________________________/ ;
\_________________ unticked_statement _______________________________________________/
statement
( You can look up the rules at https://github.com/php/php-src/blob/PHP-5.6.15/Zend/zend_language_parser.y )
There's no special rule for the assignment operator in this case; there simply isn't another way for the parser to match the statement, so precedence doesn't apply.
There doesn't seem to be a logical assignment operator in PHP. I would like to be able to write $a = $a || $b as $a ||= $b.
Note that this is not the same as $a |= $b, which does not short-circuit when $a evaluates to true.
Is there such functionality in PHP?
There is no ||= or &&= operator in PHP, there are some languages that use this (such as ruby) but they've implemented it differently.
So the only way to do it is like so:
$a = $b || $c;
You can also use the ?? operator, it means if isset then use.
$a = $b ?? $c;
Or chain values to it and use the last as default (if none of the previous evaluate to true):
$a = $b ?? $c ?? true;
Logical assignment operators only allow you to store a true or a false value and, most of the time, you can directly put (and optionally set) it in the if statement directly to save a line of code.
PHP doesn't appear to have this functionality. There is nothing in the Assignment Operator documentation or in the Logical Operators documentation that mentions this functionality. Also, it isn't included in the top rated comment on the Assignment Operator page, which is a list that someone compiled of all of the assignment operators from information in the other pages.
I've seen the next sentence in PHP code:
$a OR $a = $b;
What is the behavior of this code?
or
What is the purpose of this code?
Thanks,
The operator "OR" is a Logical operator, used to check if one of two (or more) terms is true. if either $a or ($a = $b) is TRUE.
The operator "==" (in your question is =, but i think is == ) is a Comparison operator, used to check if $a is equal to $b (like same value, term A is true and term B is true, others cases).
Thanks
This question already has answers here:
Difference between "not equal" operators <> and != in PHP
(5 answers)
Closed 9 years ago.
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?
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. 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...
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.
As mentioned at the documentation website, <>and !=are just synonyms. That means they are completely interchangeable. The history of php is a bit wild, so naming conventions, even to the point how operators are to be called, were and still are not really unified.
According to PHP manual: http://fr.php.net/manual/en/language.operators.comparison.php
it does not seem to have any difference.
There is no difference. I guess <> is something that was added in a later version of php. Kind of reminds me of Python. I think it the same with using AND or && for the and operator
It isn't any different, but I think I remember != was faster once, because I ran a test and found out <> was executing the "diff" methods of the objects I was comparing, which can be slower than the "compare" methods.
$foo = 0;
if($foo == 'on') $foo = 1;
echo $foo;
It should be expected the above code outputs "0". However it doesn't, somehow $foo == 'on' results in TRUE, although this obviously is wrong. Replacing the expression with $foo === 'on' gives the right answer, so any suspicions this might be some typing issue seem to be confirmed.
Nevertheless, how can PHP think $foo was 'on' if $foo and 'on' even aren't of the same type? Is this a bug or some weird feature?
In php is the loose comparison (==) of a string and the int 0 evaluated as True. While the strict comparison (===) also compares for the same types, that means it is compared if booth variables are strings or ints. But this evaluated as false, because $foo is an int and 'on' is a string.
Also see the comparison tables on php.net: http://php.net/types.comparisons
this is a documented behaviour:
If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement.