[英]Comparing two strings with OR in PHP
我正在嘗試比較兩個字符串,而且我在PHP中使用OR運算符
這是代碼:
<?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";
}
?>
使用上面的代碼,我總是越來越不完美,不確定為什么嗎? 怎么了? -如果在刪除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";
}
究其原因在於德摩根的邏輯定律 。 考慮表達式A or B
,其中存在A
代表是,存在B
代表否:
A V B
!(A V B)
!A ^ !B
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.