By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

checking multiple conditions for one variable

P: n/a
LRW
What I need to do is a comparison to see if a variable equals none of
the 48 contiguous United States, but as efficiently as possible.

The best way I can come up with the the textbook method of :

if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
($state != "CO") ...etc ) {

Is there some way to shorten that a bit to do:

if ($state != "AR" && "AK" && "AL" && ...etc ) {

I looked at php.net for logical operators but couldn't find anything
that seemed to apply.

If someone can just tell me the keyword to look up in php.net I'd
appreciate it.
Like "search 'foobar', newbie" would be fine. =)

Thanks!!
Liam
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
LRW wrote:
What I need to do is a comparison to see if a variable equals none of
the 48 contiguous United States, but as efficiently as possible.

The best way I can come up with the the textbook method of :

if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
($state != "CO") ...etc ) {

Is there some way to shorten that a bit to do:

if ($state != "AR" && "AK" && "AL" && ...etc ) {

I looked at php.net for logical operators but couldn't find anything
that seemed to apply.

If someone can just tell me the keyword to look up in php.net I'd
appreciate it.
Like "search 'foobar', newbie" would be fine. =)

Thanks!!
Liam


"array_search" is your keyword:

array_search() example
<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>

Greetings, Urs

--
+-------------------------
| Urs Weder
| N 4723'23" E 939'47"
+-------------------------
( modify address for return email )
Jul 17 '05 #2

P: n/a
LRW (de**@celticbear.com) wrote:
: What I need to do is a comparison to see if a variable equals none of
: the 48 contiguous United States, but as efficiently as possible.

: The best way I can come up with the the textbook method of :

: if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
: ($state != "CO") ...etc ) {

: Is there some way to shorten that a bit to do:

: if ($state != "AR" && "AK" && "AL" && ...etc ) {

: I looked at php.net for logical operators but couldn't find anything
: that seemed to apply.

: If someone can just tell me the keyword to look up in php.net I'd
: appreciate it.
: Like "search 'foobar', newbie" would be fine. =)

something like

$valid_states["AR"]=1;
$valid_states["AK"]=1;
$valid_states["AL"]=1;
$valid_states["CO"]=1;
# ...etc...

$state = "AB";
if ( $valid_states[ $state ] == 1 )
{
echo "$state is valid\n";
}

$state = "CO";
if ( $valid_states[ $state ] == 1 )
{
echo "$state is valid\n";
}
Jul 17 '05 #3

P: n/a

"LRW" <de**@celticbear.com> wrote in message
news:3a**************************@posting.google.c om...
What I need to do is a comparison to see if a variable equals none of
the 48 contiguous United States, but as efficiently as possible.

The best way I can come up with the the textbook method of :

if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
($state != "CO") ...etc ) {

Is there some way to shorten that a bit to do:

if ($state != "AR" && "AK" && "AL" && ...etc ) {

I looked at php.net for logical operators but couldn't find anything
that seemed to apply.

If someone can just tell me the keyword to look up in php.net I'd
appreciate it.
Like "search 'foobar', newbie" would be fine. =)


define (STATES "AR,AK,AL,NY,");

$state = ucase($state);
if(! strpos(STATES , $state))
{

}

you can use stripos in php5
Jul 17 '05 #4

P: n/a
> The best way I can come up with the the textbook method of :

if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
($state != "CO") ...etc ) {

Is there some way to shorten that a bit to do:

if ($state != "AR" && "AK" && "AL" && ...etc ) {


Try the following:

<?php

$state = 'CA';
$states = array('AR','AK','AL','CO');

if ( in_array($state, $states) ) {
printf('I have found : %s in : %s', $state, join(',', $states));
} else {
printf('I did not find : %s in : %s', $state, join(',', $states));
}

?>

--Wil
Jul 17 '05 #5

P: n/a
"Malcolm Dew-Jones" <yf***@vtn1.victoria.tc.ca> wrote in message
news:40******@news.victoria.tc.ca...
LRW (de**@celticbear.com) wrote:
: What I need to do is a comparison to see if a variable equals none of
: the 48 contiguous United States, but as efficiently as possible.

: The best way I can come up with the the textbook method of :

: if (($state != "AR") && ($state != "AK") && ($state != "AL") &&
: ($state != "CO") ...etc ) {

: Is there some way to shorten that a bit to do:

: if ($state != "AR" && "AK" && "AL" && ...etc ) {

: I looked at php.net for logical operators but couldn't find anything
: that seemed to apply.

: If someone can just tell me the keyword to look up in php.net I'd
: appreciate it.
: Like "search 'foobar', newbie" would be fine. =)

something like

$valid_states["AR"]=1;
$valid_states["AK"]=1;
$valid_states["AL"]=1;
$valid_states["CO"]=1;
# ...etc...

$state = "AB";
if ( $valid_states[ $state ] == 1 )
{
echo "$state is valid\n";
}

$state = "CO";
if ( $valid_states[ $state ] == 1 )
{
echo "$state is valid\n";
}


Yup, I was going to suggest that. A hash lookup is a lot quicker than
array_search() or in_array(). To simplify the code I would do this:

$states = array("AR", "AK", "AL", "CO" ... );
$states_inv = array_flip($states);
if(isset($states_inv[$state])) {
echo "$state is valid\n";
}

But since there are only 50 items though, perhaps in_array() isn't that bad.
Jul 17 '05 #6

P: n/a
LRW
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<Yf********************@comcast.com>...
"Malcolm Dew-Jones" <yf***@vtn1.victoria.tc.ca> wrote in message
news:40******@news.victoria.tc.ca...
something like

$valid_states["AR"]=1;
$valid_states["AK"]=1;
$valid_states["AL"]=1;
$valid_states["CO"]=1;
# ...etc...

$state = "AB";
if ( $valid_states[ $state ] == 1 )
{
echo "$state is valid\n";
}


Yup, I was going to suggest that. A hash lookup is a lot quicker than
array_search() or in_array(). To simplify the code I would do this:

$states = array("AR", "AK", "AL", "CO" ... );
$states_inv = array_flip($states);
if(isset($states_inv[$state])) {
echo "$state is valid\n";
}

But since there are only 50 items though, perhaps in_array() isn't that bad.


Thanks, some great info!
Much appreciated!

Liam
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.