50

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 &&??

Mike
  • 1,853
  • 3
  • 45
  • 75
Alfred
  • 21,058
  • 61
  • 167
  • 249

11 Answers11

137

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";
}
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
63
if($var == "abc" || $var == "def" || ...)
{
    echo "true";
}

Using "Or" instead of "And" would help here, i think

Tokk
  • 4,499
  • 2
  • 31
  • 47
16

you can use in_array function of php

$array=array('abc', 'def', 'hij', 'klm', 'nop');

if (in_array($val,$array))
{
  echo 'Value found';
}
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
13

Dont know, why you want to use &&. Theres an easier solution

echo in_array($var, array('abc', 'def', 'hij', 'klm', 'nop'))
      ? 'yes' 
      : 'no';
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
7

you can use the boolean operator or: ||

if($var == 'abc' || $var == 'def' || $var == 'hij' || $var == 'klm' || $var == 'nop'){
    echo "true";
}
sauerburger
  • 4,569
  • 4
  • 31
  • 42
6

You can try this:

<?php
    echo (($var=='abc' || $var=='def' || $var=='hij' || $var=='klm' || $var=='nop') ? "true" : "false");
?>
Osh Mansor
  • 1,232
  • 2
  • 20
  • 42
5

I found this method worked for me:

$thisproduct = "my_product_id";
$array=array("$product1", "$product2", "$product3", "$product4");
if (in_array($thisproduct,$array)) {
    echo "Product found";
}
5

Sorry to resurrect this, but I stumbled across it & believe it adds value to the question.

In PHP 8.0.0^ you can now use the match expression like so:

<?php
echo match ($var) {
    'abc','def','hij','klm' => 'true',
};
?>
//echos 'true' as a string

Working link from OnlinePHPfunctions

PHP Manual

Shaun Moore
  • 102
  • 1
  • 12
  • 3
    You will also need `default => 'false'` with your match cases, otherwise an exception `UnhandledMatchError` is thrown for any value not in the list. – Jason Aug 09 '22 at 16:41
1

Try this piece of code:

$first = $string[0]; 
if($first == 'A' || $first == 'E' || $first == 'I' || $first == 'O' || $first == 'U') {
   $v='starts with vowel';
} 
else {
   $v='does not start with vowel';
}
Gnqz
  • 3,292
  • 3
  • 25
  • 35
paul
  • 11
  • 1
0

It will be good to use array and compare each value 1 by 1 in loop. Its give advantage to change the length of your tests array. Write a function taking 2 parameters, 1 is test array and other one is the value to be tested.

$test_array = ('test1','test2', 'test3','test4');
for($i = 0; $i < count($test_array); $i++){
   if($test_value == $test_array[$i]){
       $ret_val = true;
       break;
   }
   else{
       $ret_val = false;
   }
}
-9

I don't know if $var is a string and you want to find only those expressions but here it goes either way.

Try to use preg_match http://php.net/manual/en/function.preg-match.php

if(preg_match('abc', $val) || preg_match('def', $val) || ...)
   echo "true"
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Orgmir
  • 383
  • 3
  • 8