PHP If Statement conditions not working

浪尽此生 提交于 2019-12-25 09:36:55

问题


if($InboundTextBody === 'Help' OR 'help'){
   echo 'help';
} 
elseif($InboundTextBody === 'Reset'){
      echo 'send reset message';
}
elseif($InboundTextBody === 'Start'){
       echo 'send start message';
}

if($InboundTextBody === 'Help' || 'help'){
   echo 'help';
} 

If my $InboundTextBody has the word Help. It will echo Help. If the $InboundTextBody has the word Reset it still echo help. it never echo reset. I need the or in my statement just in case the $InboundTextBody text has up case or lower case text


回答1:


The correct syntax for your IF is

if($InboundTextBody === 'Help' OR $InboundTextBody === 'help'){

Or you could convert all to lowercase and do

if(strtolower($InboundTextBody) === 'help'){



回答2:


You can use function strtolower for avoiding the check on UPPER and LOWER case.

if(strtolower($InboundTextBody) === 'help')

By the way, the correct IF form is:

if($InboundTextBody === 'Help' OR $InboundTextBody === 'help')



回答3:


A way to do it would be to use the built-in case-insensitive string comparison function, strcasecmp. (php.net doc here)

if (strcasecmp($InboundTextBody, 'help') == 0) {
    echo 'help';
} elseif (strcasecmp($InBoundTextBody, 'reset') == 0) {
    echo 'reset';
}

Note that the result of strcasecmp will be 0 if they ARE the same.

Please note that in your solution, if($InboundTextBody === 'Help' || 'help') does not check what if $InboundTextBody is either. It only checks if it is 'Help', but after the ||, you have the equivalent of: if ('help') which will always return true, since 'help' is not falsy (e.g. not a 0 or null).



来源:https://stackoverflow.com/questions/44739757/php-if-statement-conditions-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!