php if (not this or not this) {做某事}

标签 php if-statement

我尝试了很多次,但没有用。 我想做第一个小鸡,但第二个不工作。请帮助我

此代码有效。

<?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/

相关文章:

javascript - 无法使用 AJAX 修改表单内的元素

javascript - 遍历对象构造函数

java - "if"语句的问题

php - 从 mysqli_fetch_array 获取行号 (int)

java - 如何判断字符串中的某个字符是否为大写

C++,一种检查 3 个数组的更简单方法

coding-style - 处理超过 2 个可能的返回值?

php - 如何通过其键获取 WooCommerce 购物车项目数据数组?

php - 如何从搜索结果中排除 wordpress 页面模板(自定义模板)?

php - 内容和页脚空间问题