PHP Check a Variable Against a list of variables

Is there a simple way to write the following;
if(($a == $b) || ($a == $c) || ($a == $d) || ($a == $e)) {
do something.
}

Open in new window

LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nicolas van de WalleCommented:
You could maybe try:
if($a == ($b || $c || $d)){
    echo "OK";
}

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
In general, you should write your code so You can read it.  The PHP interpreter "pre-compiles" your code to optimize it so it isn't really important to PHP how you write it.  I tend to write it out like your example because I can easily see what I was trying to do when I have to go back to it and change something.

PS: Except of course it needs to be correct code...
0
rgranlundAuthor Commented:
@Dave;
PHP interpreter "pre-compiles"  Can you show me an example of this?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

zephyr_hex (Megan)DeveloperCommented:
If you have the values that you want to compare against in an array, you could use in_array()

You can add your values to an array using array_push()

So, you would have something like:

$stack = [];
array_push($stack,$b,$c,$d,$e);
if(in_array($a,$stack)) {
//do something magical
}

Open in new window

0
Dave BaldwinFixer of ProblemsCommented:
PHP interpreter "pre-compiles"  Can you show me an example of this?
No, that's an 'internal' operation that occurs whenever you request a page.  Here is some vague info about the Zend Engine that 'powers' PHP.

http://php.net/manual/en/internals2.opcodes.php

http://php.net/manual/en/tokens.php

And 'bcompiler' uses the 'bytecodes' from the pre-compiler.
http://php.net/manual/en/book.bcompiler.php
0
zephyr_hex (Megan)DeveloperCommented:
In general, you should write your code so You can read it

There's a balance here.  What may be easy for You to read may not be easy for Me to maintain after You have been run over by a bus.  Code should be readable, as well as maintainable.

In this particular instance, it really depends on how many variables we're comparing, and how many times that comparison is referenced.  At some point, it may be better to handle the comparisons in a manner other than the OP's example.
0
Ray PaseurCommented:
This question is data-dependent, but we don't have the data, so I'll show you how I might address this with a small data set.  The principles might be the same with a large data set, too.

<?php // demo/temp_rgranlund.php
/**
 * http://www.experts-exchange.com/questions/28814559/PHP-Check-a-Variable-Against-a-list-of-variables.html
 *
 * http://php.net/manual/en/function.in-array.php
 */
error_reporting(E_ALL);
echo '<pre>';

// AN ARRAY OF LANGUAGES
$languages =
[ 'US'
, 'FR'
, 'DE'
, 'ES'
]
;

// THE REQUEST VARIABLE
$q = !empty($_GET['q']) ? strtoupper(trim($_GET['q'])) : NULL;

// CAN WE USE THE REQUEST VARIABLE?
if (in_array($q, $languages))
{
    echo PHP_EOL . "The requested language is $q";
}
else
{
    echo PHP_EOL . "The requested language is unknown";
}

Open in new window

0
Julian HansenCommented:
Ultimately, irrespective of which method you use, $a has to be compared to the other 3 variables
I am with Dave on this - write the code so you can read it.
So you can use in_array - but that just means more code to get the data into the array in the first place and at the end of the day it ends up doing the same thing.
Personally I would leave it as it is.
0
zephyr_hex (Megan)DeveloperCommented:
@Julian - in_array means more code to get the elements into the array, but in some circumstances, it's more maintainable.  If, for example, the comparison must be done more than once, or, if, for example, you have a list of 50 elements that need to be compared and now you have a new element to add to that list, it's much easier to add that new element to the array and move on with your life than to scroll through the line of mega-comparisons to add your new element to the end.  

If we're only talking a handful of elements, then yes, there isn't a good reason to move it to an array structure.
0
Julian HansenCommented:
If, for example, the comparison must be done more than once, or, if, for example, you have a list of 50 elements that need to be compared and now you have a new element to add to that list, it's much easier to add that new element to the array and move on with your life than to scroll through the line of mega-comparisons to add your new element to the end.


If - that word can be used to pretty much negate anything.

The question asked if there was a better way to do
if(($a == $b) || ($a == $c) || ($a == $d) || ($a == $e)) {

Open in new window


The answer was given in the context of the question asked - when you introduce if - the solution set becomes so wide it is almost meaningless to have a discussion.

For example: if you only wanted to compare one item then there would be no discussion of using in_array at all - if changes the context of the question
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
One last comment, then I'll sign off on this discussion.  Whenever I see code like this:
if(($a == $b) || ($a == $c) || ($a == $d) || ($a == $e)) {
do something.
}

Open in new window

I am reminded of the concept (and reasons to avoid) cyclomatic complexity.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.