在1 if语句中是否可以有2个条件?

时间:2011-07-01 18:40:05

标签: php

我目前有以下代码行

elseif($_POST['aspam'] != 'fire'){
print "Is ice really hotter than fire?";
}

PHP中是否有任何OR函数?好像要说... ...

$_POST['aspam'] != 'fire' OR !='Fire'

或者让我的价值不区分大小写? 希望这是有道理的......

13 个答案:

答案 0 :(得分:3)

不确定

$_POST['aspam'] != 'fire' or $_POST['aspam'] !='Fire'

请记住,每个条件都是分开的。说or != 'Fire'并不会自动将其解释为or $_POST['aspam'] != 'Fire'

他们被称为logical operators

比较小写字母:

strtolower($_POST['aspam'] != 'fire'

答案 1 :(得分:3)

||or(小写)运算符。

elseif($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire'){
    print "Is ice really hotter than fire?";
}

答案 2 :(得分:3)

你可以这样做两个条件:

if($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire')

如果我是你在这种情况下,我会这样做:

if(strtolower($_POST['aspam']) != 'fire')

答案 3 :(得分:2)

使用OR创建了一个PHP ||AND创建了&&等等。所以您的代码示例如下:

if ( ($_POST['aspam'] != 'fire') || ($_POST['aspam'] != 'Fire') )

但是在你的情况下,最好是:

if (strtolower($_POST['aspam']) != 'fire')

答案 4 :(得分:2)

PHP中有不同的逻辑运算符。

用于“或”两个管道:||

$_POST['aspam'] != 'fire' || !='Fire'

以下是与所有运营商的链接: http://www.w3schools.com/PHP/php_operators.asp

答案 5 :(得分:1)

if (first condition || second condition){
your code
}

OR由2个管道表示 - ||

更多: 你也可以有AND:

if(first condition && second condition){
Your code...
}

所以并且由&&

代表

答案 6 :(得分:1)

这是逻辑OR

$_POST['aspam'] != 'fire' || !='Fire'

这是不区分大小写的(ToLower函数)

strtolower($_POST['aspam']) != 'fire'

答案 7 :(得分:1)

使用strtolower($_POST['aspam'] )!='fire'

答案 8 :(得分:1)

如果要检查变量不区分大小写,可以使用下面的代码

if(strtolower($_POST['aspam'])!='fire')
   echo "this is matching";

答案 9 :(得分:1)

  

或者说我的价值不是   区分大小写?

if (strtolower($_POST['aspam']) != 'fire'){

}

答案 10 :(得分:1)

是的,这是可能的。试试这个:

elseif($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire')

答案 11 :(得分:0)

您可以使用不区分大小写的字符串比较:

if (strcasecmp($_POST['aspam'], 'fire') !== 0) {
    print "Is ice really hotter than fire?"; 
}

或列表:

if (!in_array($_POST['aspam'], array('Fire','fire')) {
    ...

答案 12 :(得分:0)

这里最短的选项可能是stristr

if (stristr($_POST["aspam"], "FIRE")) {

它会进行不区分大小写的搜索。要使其成为固定长度匹配,您可能需要strcasecmpstrncasecmp。 (但是我发现它的可读性较差,在您的情况下看起来不太必要。)