tags:

views:

97

answers:

5

Hi All

Quick PHP question - is there a simpler way to write the following query (to see if any of our three variables are equal to the same value):

if ($firstvariable == 'ourvalue' OR $secondvariable == 'ourvalue' OR $thirdvariable == 'ourvalue')
+3  A: 

Nope. That's about as simple as it gets.

Mike Caron
Why did someone vote this down? The other answers are not simpler than the OP's question.
Mike Caron
no?.. is "ourvalue" written 3 times in the other answers? are there 3 equality operators and 2 logical ORs in the other answers?
Fosco
Are they creating arrays, searching arrays and discarding arrays when equality operators are all that are necessary?
Mike Caron
I specifically said that performance wise, it's not better. I guess it's up to Sam what he meant by simpler.
Fosco
@Mike well, why have functions if we can use goto, right? Sometimes performance is not that important. Is up to the guy who implements it to decide if the performance hits is worth it.
AlbertEin
Well, in this particular case, my query is exactly as above - just 3 variables, one value they need to equal. So I guess the answer in this case is no, there isn't a simpler (or faster) way. But I guess if the amount of variables increased, then adding them to an array might be a better option, so thanks for the other answers - I'm sure I will use them in the future.
Sam
@Albert Straw man argument aside, it depends. I can think of situations where goto would be better. But, why use an array when you just want to compare three variables? If it were 10, or 50 variables, then sure the array would be simpler. But, three? No way.
Mike Caron
+3  A: 

Try this:

if (in_array('ourvalue', array($first, $second, $third))) doSomething ();

What we are doing is to create an array that contains the values of the variables that you want to test and we then let the in_array function to seek the needle value ('ourvalue') in the array that we've just created.

AlbertEin
You were missing a `)`, fixed.
chpwn
+1 for thinking like me.
Fosco
@chpwn Thank you for the missing ).@Fosco :)
AlbertEin
+4  A: 

Performance-wise, this is likely a bad idea, but to remove code duplication I think this would work:

if (in_array("ourvalue",array($firstvariable,$secondvariable,$thirdvariable))) {
Fosco
+1 `in_array()` doesn't have to be limited to *checking a variable* for certain hardcoded *values* :)
BoltClock
I would hardly call this simpler.
Mike Caron
+1 For thinking like me :p :)
AlbertEin
+1  A: 
if (in_array("ourvalue",array($firstvariable,$secondvariable,$thirdvariable)))
    echo "TEST";
dale
A: 

Here's a quick trick: you can use Wolfram Alpha to simplify some logical expressions, for example: (x==a || a==b || x==x) can be simplified to "True"

Trying with the example you give, it appears not to be able to be simplified.

Adam
Sorry, but this is not the case, the expression here is `(a == x || a == y || a == z)
AlbertEin
Uh, I think you've mis-read my answer. I wasn't offering a concrete solution, but rather an example of how Wolfram Alpha can simplify logical expressions.
Adam