【问题标题】:What is the negation of the AND operator when using if and else?使用 if 和 else 时 AND 运算符的否定是什么?
【发布时间】:2024-01-24 03:27:01
【问题描述】:

我将if&& 一起使用,然后是else

例子:

$Name = "john";
$age = "30";

如果我这样做:

if($Name =="john" && $age=="30") 
{ some stuff }
else { do other stuff }

这里的 else 是指:$Name !="john" && $age != "30" 吗?我对此有点困惑。

谢谢

【问题讨论】:

标签: php if-statement operators


【解决方案1】:
$Name == "john";
$age == "30";

if($Name =="john" && $age=="30") 
{
   // Here comes only when name is john and age is 30
}
else 
{ 
  // Here comes all the time when name is not john AND age is not 30
  // If age is 30 and name is not John then comes here
  // If age is not 30 and name is John then comes here
}

【讨论】:

    【解决方案2】:

    && 运算符仅在所有条件都为真时才有效

    if($Name =="john" && $age=="30") {// both are true}
    

    其他条件是其中一个为假或两者均为假,如以下条件...

    ($Name !="john" && $age == "30")
    ($Name == "john" && $age != "30")
    ($Name != "john" && $age != "30)
    

    【讨论】:

      【解决方案3】:

      你可以画一个真值表来更好地理解你在更复杂情况下的表达:

      $Name=="john" ? | $age=="30" ? | ($Name =="john" && $age=="30") 
      ----------------+--------------+-------------------------------
              0  (no) |      0  (no) |                     0 (false)
              0  (no) |      1 (yes) |                     0 (false)
              1 (yes) |      0  (no) |                     0 (false)
              1 (yes) |      1 (yes) |                     1  (true)
      

      【讨论】:

        最近更新 更多