简体   繁体   English

在PHP中使用OR比较两个字符串

[英]Comparing two strings with OR in PHP

I am trying to compare two strings and also I am using OR operator in PHP 我正在尝试比较两个字符串,而且我在PHP中使用OR运算符

Here is the Code: 这是代码:

<?php

    $admintext= "Yes";

    if(strcmp($admintext, 'Yes') != 0 or strcmp($admintext, 'No') != 0)
    {
         echo " Not perfect, Admin text doesn't have Yes OR No text"; 
    }
    else
    {
        echo " Perfect, Admin text has Yes or No";
    }
?>

with the above code, I am always getting Not perfect, not sure why? 使用上面的代码,我总是越来越不完美,不确定为什么吗? What is wrong? 怎么了? - If I remove code after OR this work perfectly. -如果在删除OR后删除代码,则效果很好。

Thanks! 谢谢!

You need to join the conditions with and , nor or : 您需要使用and ,或or来加入条件:

if (strcmp($admintext, 'Yes') != 0 and strcmp($admintext, 'No') != 0) {
     echo " Not perfect, Admin text doesn't have Yes OR No text"; 
}
else {
    echo " Perfect, Admin text has Yes or No";
}

The reason for lies in the DeMorgan's logic laws . 究其原因在于德摩根的逻辑定律 Consider the expression A or B , where A represent yes is present, and B represents no is present: 考虑表达式A or B ,其中存在A代表是,存在B代表否:

A V B
!(A V B)
!A ^ !B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM