# If Or

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I need to test for one of several values. Can I include these in a
single "if" statement

I tried using OR but it does not work. In the actual file I need to
check if a supplied user input meets one of about 170 possible code
combinations.

This does not work

if (\$var == 1 OR 3 OR 7 OR 8 OR 9 OR "Table" OR "Chair"){
echo 'yes';
} else {
echo 'no';
}

A long winded way to do it and the only way I know how to use OR is...

if (\$var == 1 || \$var == 3 || \$var == 7 || \$var == 7 || \$var ==  9 ||
\$var == "Table" || \$var == "Chair")
...etc

But I would like to just list the values (and not the \$var == for
every possible condition

Garry Jones
Sweden

## Re: If Or

GarryJones contained the following:

Put the values in an array and use in_array()

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

## Re: If Or

GarryJones wrote:

if ( in_array( \$var, array( 1, 3, 7, 9) ) {
// do something
}

## Re: If Or

Hello,

If you have array of all possible 170 values then you can try with
in_array function. If you don't have array then prepare it and then
check it.

## Re: If Or

well, this is the best i can offer based on what you've described...

function checkInput(\$value)
{
static \$combination = array(
1           ,
3           ,
7           ,
8           ,
9           ,
'Table'     ,
'Chair'
)
return in_array(\$value, \$combination, true);
}

echo checkInput(\$var) ? 'yes' : 'no';

that's one way. and, you could build \$combination from a db so that the
function scales. another way would be to write (or store and retrieve) a
regular expression or series of them:

/^[13789]|table|chair\$/gi

then you'd preg_match a true or false answer from that.

hth,

me

|I need to test for one of several values. Can I include these in a
| single "if" statement
|
| I tried using OR but it does not work. In the actual file I need to
| check if a supplied user input meets one of about 170 possible code
| combinations.
|
| This does not work
|
| if (\$var == 1 OR 3 OR 7 OR 8 OR 9 OR "Table" OR "Chair"){
| echo 'yes';
| } else {
| echo 'no';
| }
|
| A long winded way to do it and the only way I know how to use OR is...
|
| if (\$var == 1 || \$var == 3 || \$var == 7 || \$var == 7 || \$var ==  9 ||
| \$var == "Table" || \$var == "Chair")
| ...etc
|
| But I would like to just list the values (and not the \$var == for
| every possible condition
|
| Garry Jones
| Sweden
|

## Re: If Or

On 18.04.2007 14:58 GarryJones wrote:

Hardcoding more than 100 conditions doesn't seem to be a good idea in
any case. Put them in db and use the power of sql to find if the value
matches.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok