Is it possible to have 2 conditions within 1 if statement?
I currently have the following line of code
elseif($_POST['aspam'] != 'fire'){
print "Is ice really hotter than fire?";
}
Is there any sort of OR funct开发者_高级运维ion within PHP? as if to say if...
$_POST['aspam'] != 'fire' OR !='Fire'
OR alternatively make my value not case sensitive? Hopefully this makes sense...
The ||
or or
(lowercase) operator.
elseif($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire'){
print "Is ice really hotter than fire?";
}
Sure.
$_POST['aspam'] != 'fire' or $_POST['aspam'] !='Fire'
Just remember that each condition is separate. Saying or != 'Fire'
doesn't automatically interpret it as or $_POST['aspam'] != 'Fire'
.
They're called logical operators.
To compare the lowercase:
strtolower($_POST['aspam'] != 'fire'
You can do two conditions like this:
if($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire')
If I were you in this case, I would do:
if(strtolower($_POST['aspam']) != 'fire')
A PHP OR
is created with ||
, AND
created with &&
, etc. So your code example would look like:
if ( ($_POST['aspam'] != 'fire') || ($_POST['aspam'] != 'Fire') )
However in your case it would be better to:
if (strtolower($_POST['aspam']) != 'fire')
There are different logical operators in PHP.
Use for "OR" two pipes: ||
$_POST['aspam'] != 'fire' || !='Fire'
Here's a link with all operators: http://www.w3schools.com/PHP/php_operators.asp
Yes.
if (first condition || second condition){
your code
}
The OR is represented by 2 pipes - ||
Something more: You can also have AND:
if(first condition && second condition){
Your code...
}
So and is represented by &&
This is the logical OR
$_POST['aspam'] != 'fire' || !='Fire'
and this is the case-insensitive (ToLower function)
strtolower($_POST['aspam']) != 'fire'
Use strtolower($_POST['aspam'] )!='fire'
If you want to make the checking of your variable case insensitive, you can use below code
if(strtolower($_POST['aspam'])!='fire')
echo "this is matching";
OR alternatively make my value not case sensitive?
if (strtolower($_POST['aspam']) != 'fire'){
}
Yes, this is possible. Try this:
elseif($_POST['aspam'] != 'fire' || $_POST['aspam'] != 'Fire')
You could use case insensitive string comparison:
if (strcasecmp($_POST['aspam'], 'fire') !== 0) {
print "Is ice really hotter than fire?";
}
Or a list:
if (!in_array($_POST['aspam'], array('Fire','fire')) {
...
The shortest option here would probably be stristr
:
if (stristr($_POST["aspam"], "FIRE")) {
It does a case-insensitive search. To make it a fixed-length match you might however need strcasecmp
or strncasecmp
in your case. (However I find that less readable, and doesn't look necessary in your case.)
精彩评论