Reputation: 2785
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!
Upvotes: 0
Views: 79
Reputation: 521339
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
Upvotes: 3