'AND' vs '&&' as operator

2018-12-31 05:27发布

I have a codebase where developers decided to use AND and OR instead of && and ||.

I know that there is a difference in operators' precedence (&& goes before and), but with the given framework (PrestaShop to be precise) it is clearly not a reason.

Which version are you using? Is and more readable than &&? Or is there no difference?

11条回答
公子世无双
2楼-- · 2018-12-31 05:59

Another nice example using if statements without = assignment operations.

if (true || true && false); // is the same as:
if (true || (true && false)); // TRUE

and

if (true || true AND false); // is the same as:
if ((true || true) && false); // FALSE

because AND has a lower precedence and thus || a higher precedence.

These are different in the cases of true, false, false and true, true, false. See https://ideone.com/lsqovs for en elaborate example.

查看更多
与君花间醉酒
3楼-- · 2018-12-31 06:00

Precedence differs between && and and (&& has higher precedence than and), something that causes confusion when combined with a ternary operator. For instance,

$predA && $predB ? "foo" : "bar"

will return a string whereas

$predA and $predB ? "foo" : "bar"

will return a boolean.

查看更多
刘海飞了
4楼-- · 2018-12-31 06:00

Let me explain the difference between “and” - “&&” - "&".

"&&" and "and" both are logical AND operations and they do the same thing, but the operator precedence is different.

The precedence (priority) of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator.

Mixing them together in single operation, could give you unexpected results in some cases I recommend always using &&, but that's your choice.


On the other hand "&" is a bitwise AND operation. It's used for the evaluation and manipulation of specific bits within the integer value.

Example if you do (14 & 7) the result would be 6.

7   = 0111
14  = 1110
------------
    = 0110 == 6
查看更多
与君花间醉酒
5楼-- · 2018-12-31 06:00

Depending on the language that you are using, generally it is better practice to use && and || rather than and and or, except in languages like Python, where and and or are used instead and && and || do not exist.

查看更多
若你有天会懂
6楼-- · 2018-12-31 06:02

Here's a little counter example:

$a = true;
$b = true;
$c = $a & $b;
var_dump(true === $c);

output:

bool(false)

I'd say this kind of typo is far more likely to cause insidious problems (in much the same way as = vs ==) and is far less likely to be noticed than adn/ro typos which will flag as syntax errors. I also find and/or is much easier to read. FWIW, most PHP frameworks that express a preference (most don't) specify and/or. I've also never run into a real, non-contrived case where it would have mattered.

查看更多
十年一品温如言
7楼-- · 2018-12-31 06:03

If you use AND and OR, you'll eventually get tripped up by something like this:

$this_one = true;
$that = false;

$truthiness = $this_one and $that;

Want to guess what $truthiness equals?

If you said false... bzzzt, sorry, wrong!

$truthiness above has the value true. Why? = has a higher precedence than and. The addition of parentheses to show the implicit order makes this clearer:

($truthiness = $this_one) and $that

If you used && instead of and in the first code example, it would work as expected and be false.

As discussed in the comments below, this also works to get the correct value, as parentheses have higher precedence than =:

$truthiness = ($this_one and $that)
查看更多
登录 后发表回答