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')
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')
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.
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))) {
if (in_array("ourvalue",array($firstvariable,$secondvariable,$thirdvariable)))
echo "TEST";
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.