PHP If Statement with Multiple Conditions
I have a variable$var
.
I want echo "true"
if $var
is equal to any of the following values abc
, def
, hij
, klm
, or nop
. Is there a way to do this with a single statement like &&
??
An elegant way is building an array on the fly and using in_array()
:
if (in_array($var, array("abc", "def", "ghi")))
The switch
statement is also an alternative:
switch ($var) {
case "abc":
case "def":
case "hij":
echo "yes";
break;
default:
echo "no";
}
if($var == "abc" || $var == "def" || ...)
{
echo "true";
}
Using "Or" instead of "And" would help here, i think