In PHP, is there a short way to compare a variable to multiple values?
Asked Answered
F

5

24

Basically what I'm wondering if there is a way to shorten something like this:

if ($variable == "one" || $variable == "two" || $variable == "three")

in such a way that the variable can be tested against or compared with multiple values without repeating the variable and operator every time.

For example, something along the lines of this might help:

if ($variable == "one" or "two" or "three")

or anything that results in less typing.

Fabian answered 2/5, 2013 at 19:5 Comment(1)
I realized that after I posted. Thanks for the tip, of course!Fabian
H
45

in_array() is what I use

if (in_array($variable, array('one','two','three'))) {
Haslam answered 2/5, 2013 at 19:6 Comment(5)
always too fast for me John Conde :PYesseniayester
I did realize this after I posted my question. Guess I jumped the gun. This is a rather brilliant solution and particularly helpful when comparing to several things at once. Thank you. I'll accept it once the site lets me. It says I have to wait.Fabian
@brbcoding, I still appreciate your efforts.Fabian
shorter: if (in_array($variable, ['one','two','three'])) {Antichlor
how would you handle this with an AND && situation?Hydrogen
C
4

Without the need of constructing an array:

if (strstr('onetwothree', $variable))
//or case-insensitive => stristr

Of course, technically, this will return true if variable is twothr, so adding "delimiters" might be handy:

if (stristr('one/two/three', $variable))//or comma's or somehting else
Corbel answered 2/5, 2013 at 19:28 Comment(6)
I think you have a typo and meant to say "twothr" instead of "thothr", but obviously I know what you meant. At any rate, this is another nice tactic, and in fact, is even shorter. I noticed you used strstr the first time and stristr the second time. What is the difference?Fabian
strstr looks for an exact string match (CaseSensitive) stristr with the i performs a case-insensiteve comparison. that's the only difference. And yes, that tothr rubbish was a typo :PCorbel
Ah, okay. That's what I thought the difference was. Also 'thwothr' is still a typo XDFabian
@vertigoelectric: aw nips... I just can't seem to get it together today ;)Corbel
Unfortunately that's not enough: this will still approve a value such as "ree" or "wo". You would need to use "/{$variable}/" instead of $variable. While probably slower, the in_array solution starts to appear cleaner.Willey
@LSerni: in this case one would need a string like '/one/two/three/', so the first and last fit as well.Unbelief
Y
1
$variable = 'one';
// ofc you could put the whole list in the in_array() 
$list = ['one','two','three'];
if(in_array($variable,$list)){      
    echo "yep";     
} else {   
    echo "nope";        
}
Yesseniayester answered 2/5, 2013 at 19:8 Comment(0)
B
0

With switch case

switch($variable){
 case 'one': case 'two': case 'three':
   //do something amazing here
 break;
 default:
   //throw new Exception("You are not worth it");
 break;
}
Boudreaux answered 23/10, 2014 at 13:59 Comment(0)
K
0

Using preg_grep could be shorter and more flexible than using in_array:

if (preg_grep("/(one|two|three)/i", array($variable))) {
  // ...
}

Because the optional i pattern modifier (insensitive) can match both upper and lower case letters.

Kelseykelsi answered 2/9, 2016 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.