473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

checking multiple conditions for one variable

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
6 12805
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 47°23'23" E 9°39'47"
+-------------------------
( modify address for return email )
Jul 17 '05 #2
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

"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
> 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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paris_Sucks | last post by:
I'm trying to redirect when testing for certain condidtions as shown below. When the conditions are ture, it redirects, but still goes ahead and processes the sql query. What am I doing wrong??? ...
3
by: NotGiven | last post by:
In other words if I want to check if $SESSION = 'A', do I also NEED to check isset($_SESSION) ? If so, what logical IF statement works? I f have tried for an hour to come up with a IF...
2
by: Ginu | last post by:
Hi, the task is to identify semantically identical elements where some additional attributes do not match. The XSL-transformation should find a node NAME which @id attribute matches to another...
0
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
3
by: kavithapotnuru | last post by:
Hi All, Can anyone help me how to give multiple conditions in a while loop in perl. For example i have a while loop for which the value of a variable is A or B it should not enter in to the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.