简体   繁体   中英

Comparing two strings with OR in PHP

I am trying to compare two strings and also I am using OR operator in PHP

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.

Thanks!

You need to join the conditions with and , nor 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 V B
!(A V B)
!A ^ !B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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