<?
$a=true;
$b=true;
$c=false;
if($a or $b and $c) 
echo"true";
else
echo"false";
echo "<br>";
if($a || $b and $c) 
echo"true";
else
echo"false";
?>
or和||都是逻辑或,优先级不一样,为什么输出的结果,一个为true,一个为false?

解决方案 »

  1.   

    or和||的权重是不同的,or的权重高于||,and同理比&&的权重要高,所以if($a || $b and $c)的最明白的表现形式为
    if($a || ($b and $c))  由此得出一个结论,那就是||和or,以及and和&&不可混用,除非自己很清楚此权重问题
      

  2.   

    短路运算符要权重高的在前面的,就像&&与||,要&&在前面
      

  3.   

    我也有点混乱,平时都是用 or 或者  and ,都不用||,&
      

  4.   

    <?
    $a=true;
    $b=true;
    $c=false;
    // 运算符的优先级   && > || > and > or
    if($a or $b and $c)   //  相当于 $a or ($b and &c)  => true  if($a || $b and $c)   // 相当于  ($a || $b) and $c  => false
      

  5.   

    用统一的 or and或者 ||  &&这样就简单点
    或者直接用 () 起来