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

0

11 Answers 11

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";
}
2
  • 4
    I swear in php3 I used to do if($var == 'abc'|'xyz'|'cbs') maybe it was just a dream :p
    – nodws
    Jun 12, 2017 at 17:36
  • How about multiple and &&?
    – Ray Coder
    Jul 24, 2021 at 17:53
62
if($var == "abc" || $var == "def" || ...)
{
    echo "true";
}

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

16

you can use in_array function of php

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

if (in_array($val,$array))
{
  echo 'Value found';
}
13

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

echo in_array($var, array('abc', 'def', 'hij', 'klm', 'nop'))
      ? 'yes' 
      : 'no';
0
7

you can use the boolean operator or: ||

if($var == 'abc' || $var == 'def' || $var == 'hij' || $var == 'klm' || $var == 'nop'){
    echo "true";
}
0
6

You can try this:

<?php
    echo (($var=='abc' || $var=='def' || $var=='hij' || $var=='klm' || $var=='nop') ? "true" : "false");
?>
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";
}
1
  • 1
    in_array() was recommended 7 years earlier. This code-only answer adds no new value to the page. May 16, 2019 at 6:10
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

1
  • 1
    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 9, 2022 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';
}
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"
2
  • 8
    -1 Wow! Do you know how much overhead you just caused? Good god, man! Apr 8, 2011 at 11:09
  • Not to mention the missing delimiters in in the pattern.
    – SOFe
    Jun 6, 2017 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.