21

I'd like to go from this:

if($var == 3 || $var == 4 || $var == 5 || $var =='string' || $var == '2010-05-16') {
   // execute code here
}

to this:

if($var == (3, 4, 5, 'string', '2010-05-16')) { // execute code here }

Seems very redundant to keep typing $var, and I find that it makes it a bit cumbersome to read. Is there a way in PHP to do simplify it in this way? I read on a post here that when using XQuery you can use the = operator as in $var = (1,2,3,4,5) etc.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
John
  • 221
  • 1
  • 2
  • 4

8 Answers8

43

Place the values in an array, then use the function in_array() to check if they exist.

$checkVars = array(3, 4, 5, "string", "2010-05-16");
if(in_array($var, $checkVars)){
    // Value is found.
}

http://uk.php.net/manual/en/function.in-array.php

Greg
  • 21,235
  • 17
  • 84
  • 107
  • One of the first responses, nice solution, and includes a link to the manual. Thanks! – John Nov 05 '10 at 14:52
  • 3
    [As of PHP 5.4 you can also use the short array syntax, which replaces array() with \[\]](http://www.php.net/manual/en/language.types.array.php), so just `if(in_array($var, [3,4,5,"string","2010-05-16"])){...}` suffices. –  Apr 07 '14 at 07:42
  • PHP 5.4 doesn't _replace_ `array()` with `[]` – Greg Apr 07 '14 at 12:11
9

If you need to perform this check very often and you need good performance, don't use a slow array search but use a fast hash table lookup instead:

$vals = array(
    1 => 1,
    2 => 1,
    'Hi' => 1,
);

if (isset($vals[$val])) {
    // go!
}
NikiC
  • 100,734
  • 37
  • 191
  • 225
  • 1
    This is a very interesting solution. I'll have to run a script or two to see how the speed compares, as I'm quite curious now. – John Nov 05 '10 at 14:47
  • 3
    Hmmmm ... over 6 years later and no conclusion? One begins to suspect the code must be pretty slow. :-) – Mark Goldfain Mar 05 '17 at 09:01
  • [Googling](https://www.google.com/search?q=php+performance+isset+vs.+in_array) for the performance comparison it seems `isset` indeed is much faster. There's also a post about it on SO: https://stackoverflow.com/q/13483219/2199525 – leymannx Nov 08 '19 at 09:50
7
if (in_array($var, array(3, 4, 5, 'string', '2010-05-16'))) {execute code here }

Or, alternatively, a switch block:

switch ($var) {
    case 3:
    case 4:
    case 5:
    case 'string':
    case '2010-05-16':
        execute code here;
        break;
}
tdammers
  • 20,353
  • 1
  • 39
  • 56
  • The first solution is excellent. The second option here works, just as the if statement I proposed works, but it's just as cumbersome I believe. Instead of typing $var many times, have to type case many times. Thanks for the response! – John Nov 05 '10 at 14:51
5

You can use in_array().

if (in_array($var, array(3,4,5,"string","2010-05-16"))) { .... }
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

Or you can use in_array()

if(in_array($var,array(4,5,'string','2010-05-16',true)) {

}
MatTheCat
  • 18,071
  • 6
  • 54
  • 69
1

Just to give an alternative solution to the use of in_array:

In some cases it could be faster to set an array where the values are keys and then check with isset()

Example:

$checkVars= [3 => true, 
             4 => true, 
             5 => true, 
             "string" => true, 
             "2010-05-16" => true];

if(isset($checkVars[$var])
{
   // code here
}

EDIT: I have done some testing and it looks like this method is faster in most cases.

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
0

I've had this problem and solved it by making this function:

function orEquals(){
    $args = func_get_args();

    foreach($args as $arg){
        if ($arg != $args[0]){
            if($arg == $args[0]){
                return true;
                break;
            }
        }
    }
    unset($args);
}

then you can just call the function like this:

if(orEquals($var, 3, 4, 5, 'string', '2010-05-16')){
//your code here
}
0
$vals = array (3, 4, 5, 'string', '2010-05-16');
if(in_array($var, $vals)) {
  //execute code here
}
Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30