【问题标题】:Difference between AND [and] &&AND [和] && 之间的区别
【发布时间】:2011-08-19 16:23:47
【问题描述】:

鉴于声明:

if($value && array_key_exists($value, $array)) {

         $hello['world'] = $value;

        }

最好使用逻辑运算符AND 而不是&&?

【问题讨论】:

标签: php logical-operators


【解决方案1】:

就他们自己而言,他们做的完全一样。所以a && b 的含义与a and b 相同。但是,它们并不相同,因为&& 的优先级高于and。请参阅the docs 了解更多信息。

这里的这个例子显示了不同之处:

// The result of the expression (false && true) is assigned to $e
// Acts like: ($e = (false && true))
$e = false && true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) and true)
$f = false and true;

【讨论】:

    【解决方案2】:

    您提供的链接有注释:

    “and”和“or”运算符的两种不同变体的原因是它们以不同的优先级操作。 (请参阅运算符优先级。)

    在您的情况下,由于它是唯一的运算符,这取决于您,但它们完全相同

    【讨论】:

      【解决方案3】:

      它们在条件语句中是相同的,但要小心条件赋值:

      // The result of the expression (true && false) is assigned to $g
      // Acts like: ($g = (true && false))
      $g = true && false;
      
      // The constant true is assigned to $h and then false is ignored
      // Acts like: (($h = true) and false)
      $h = true and false;
      

      // The result of the expression (false || true) is assigned to $e
      // Acts like: ($e = (false || true))
      $e = false || true;
      
      // The constant false is assigned to $f and then true is ignored
      // Acts like: (($f = false) or true)
      $f = false or true;
      

      来自您链接到的 Logical Operators 手册。

      【讨论】:

        【解决方案4】:

        只有拼写的区别..使用 && 代替 ANDand..

        【讨论】:

        • 如果你能详细说明原因就好了。
        猜你喜欢
        • 1970-01-01
        • 2018-02-27
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多