'AND' vs '&&' as operator

后端 未结 10 593
野性不改
野性不改 2020-11-22 02:42

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

I know that there is a

相关标签:
10条回答
  • 2020-11-22 03:10

    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.

    0 讨论(0)
  • 2020-11-22 03:12

    which version are you using?

    If the coding standards for the particular codebase I am writing code for specifies which operator should be used, I'll definitely use that. If not, and the code dictates which should be used (not often, can be easily worked around) then I'll use that. Otherwise, probably &&.

    Is 'and' more readable than '&&'?

    Is it more readable to you. The answer is yes and no depending on many factors including the code around the operator and indeed the person reading it!

    || there is ~ difference?

    Yes. See logical operators for || and bitwise operators for ~.

    0 讨论(0)
  • 2020-11-22 03:13

    Since and has lower precedence than = you can use it in condition assignment:

    if ($var = true && false) // Compare true with false and assign to $var
    if ($var = true and false) // Assign true to $var and compare $var to false
    
    0 讨论(0)
  • 2020-11-22 03:16

    For safety, I always parenthesise my comparisons and space them out. That way, I don't have to rely on operator precedence:

    if( 
        ((i==0) && (b==2)) 
        || 
        ((c==3) && !(f==5)) 
      )
    
    0 讨论(0)
  • 2020-11-22 03:22

    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)
    
    0 讨论(0)
  • 2020-11-22 03:26

    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.

    0 讨论(0)
提交回复
热议问题