我尝试了很多次,但没有用。 我想做第一个小鸡,但第二个不工作。请帮助我
此代码有效。
<?php
$a = 2;
if ( $a == 2 || $a == 3 ) {
echo "OK";
}else {
echo "Error !!!!!!!";
}
?>
但是这个一直没用。
<?php
$a = 2;
if ( $a !== 2 || $a !== 3 ) {
echo "Error";
}
?>
请帮帮我
最佳答案
分解:
if ( $a !== 2 || $a !== 3 ) {
echo "Error";
}
注意两点:
- 如果
$a==2
则$a!==3
。 - 如果
$a==3
则$a!==2
。
这意味着这些条件之一将始终为真,因此 if
语句将始终执行,因为 ||
是一个 OR 语句。
如果 $a
不是 2 OR 3 你想执行,那么你应该写:
if ( $a !== 2 && $a !== 3 ) {
或者:
if ( ! ( $a == 2 || $a == 3) ) {
这样,只有当 $a
既不是 2
也不是 3
时,代码才会执行。
维基百科关于 De Morgan's Laws 的文章对此进行了探讨(感谢@Marc),其中指出:
The rules allow the expression of conjunctions and disjunctions purely in terms of each other via negation.
The rules can be expressed in English as:
The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.or informally as:
"not (A and B)" is the same as "(not A) or (not B)"
also,
"not (A or B)" is the same as "(not A) and (not B)".
关于php if (not this or not this) {做某事},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35016236/