PHP AND OR 条件与 if else
PHP AND OR Condition with if else
我有一个困惑,这里有什么问题?如果设置了 A & B 或 C,它应该打印 yes,否则它应该打印错误信息
$a = 1;
$b = 2;
$c = null;
if ((is_null($a) && is_null($b)) || is_null($c)) {
echo 'A and B or C cannot be blank.';
}
else
echo 'yes';
我在这里分配了 A 和 B,但仍在打印 'A and B or C cannot be blank.'
为了更清楚。 A = 名字,B = 姓氏,C = 电子邮件。因此用户应该提供名字和姓氏,否则请发送电子邮件。
您应该按照以下方式进行操作:
$a = 1;
$b = 2;
$c = null;
if ((!is_null($a) && !is_null($b)) || !is_null($c)) {
echo 'yes';
}
else
{
echo 'A and B or C cannot be blank.';
}
更新:
if ((is_null($a) || is_null($b)) && is_null($c)) {
echo 'A and B or C cannot be blank.';
}
您已分配 A&B,但 if 条件表明 A&B 或 C 必须为真才能打印 "A and B or C cannot be blank"。在您为它们赋值时,A&B 部分是错误的。这意味着 if( false (A&B 已分配) 或 true (C 未分配)) 将导致 true 并因此打印 "A and B or C cannot be blank"
$a = 1;
$b = 2;
$c = null;
if ((is_null($a) && is_null($b)) || is_null($c)) {
echo 'yes';
}
else{
echo 'A and B or C cannot be blank.';
}
$a = 1;
$b = 2;
$c = null;
if ((empty($a) && empty($b)) || empty($c)) {
echo 'A and B or C cannot be blank.';
}
else{
echo 'yes';
}
在你原来的post中,你写了
if A & B or C is set it should print yes, otherwise it should print the error message
但您实际上是在测试未设置的变量。
更接近您的断言(也更易读)的解决方案是编写如下代码:
if (($a && $b) || $c) {
echo 'yes';
} else {
echo 'A and B or C cannot be blank.';
}
$a && $b
两边的括号不是必需的,但有助于确定预期的分组。
我有一个困惑,这里有什么问题?如果设置了 A & B 或 C,它应该打印 yes,否则它应该打印错误信息
$a = 1;
$b = 2;
$c = null;
if ((is_null($a) && is_null($b)) || is_null($c)) {
echo 'A and B or C cannot be blank.';
}
else
echo 'yes';
我在这里分配了 A 和 B,但仍在打印 'A and B or C cannot be blank.'
为了更清楚。 A = 名字,B = 姓氏,C = 电子邮件。因此用户应该提供名字和姓氏,否则请发送电子邮件。
您应该按照以下方式进行操作:
$a = 1;
$b = 2;
$c = null;
if ((!is_null($a) && !is_null($b)) || !is_null($c)) {
echo 'yes';
}
else
{
echo 'A and B or C cannot be blank.';
}
更新:
if ((is_null($a) || is_null($b)) && is_null($c)) {
echo 'A and B or C cannot be blank.';
}
您已分配 A&B,但 if 条件表明 A&B 或 C 必须为真才能打印 "A and B or C cannot be blank"。在您为它们赋值时,A&B 部分是错误的。这意味着 if( false (A&B 已分配) 或 true (C 未分配)) 将导致 true 并因此打印 "A and B or C cannot be blank"
$a = 1;
$b = 2;
$c = null;
if ((is_null($a) && is_null($b)) || is_null($c)) {
echo 'yes';
}
else{
echo 'A and B or C cannot be blank.';
}
$a = 1;
$b = 2;
$c = null;
if ((empty($a) && empty($b)) || empty($c)) {
echo 'A and B or C cannot be blank.';
}
else{
echo 'yes';
}
在你原来的post中,你写了
if A & B or C is set it should print yes, otherwise it should print the error message
但您实际上是在测试未设置的变量。
更接近您的断言(也更易读)的解决方案是编写如下代码:
if (($a && $b) || $c) {
echo 'yes';
} else {
echo 'A and B or C cannot be blank.';
}
$a && $b
两边的括号不是必需的,但有助于确定预期的分组。