Idiotic PHP question, mutiple values for If Statements

AngryJim

Limp Gawd
Joined
Apr 20, 2006
Messages
234
Let's say I had an if/elseif/else statement where each one had multiple possibilities (specifically, zip codes) is there an easier, shorter way to write than the following?

if($zip == "23923" or $zip == "39124" or $zip == "12302"......etc.)

Each one will have around 10 zip codes that could match. Thanks.
 
if($zip == "23923" or $zip == "39124" or $zip == "12302"......etc.)
PHP:
if( in_array( $zip, array("23923", "39124", "12302", "90210", "98371") ) ) {

That's how I would do it. It checks to see if $zip is in the defined array. Or you could specify the several arrays of zip codes first then just reference the name of the arrays.

PHP:
$washington = array("98007", "98008", "98009", "98010", "98011");
$california = array("90210", "90211", "90212", "90213", "90214");

if( in_array( $zip, $washington ) ) {
}elseif( in_array( $zip, $california) ) {
}else{
}
 
The in_array method is the way to go. It ends up being faster than the compounded if statements, interestingly enough.
 
If you have a large number of zip-codes, you might consider putting them in a hash-table (a string-indexed array in PHP) and using array_has_key() rather than in_array().
 
If you have a large number of zip-codes, you might consider putting them in a hash-table (a string-indexed array in PHP) and using array_has_key() rather than in_array().

So string-indexed arrays in PHP are like maps in C++? I'm pretty new to PHP, but I didn't know it had that kind of data structure; that's pretty neat.
 
i could be wrong but i think "numeric" arrays are actually implemented as maps where the key is just the number.

but yeah, php has maps/hashtables:
$map["foo"] = "bar";
 
I am not an extensive php programmer but why not use a switch statement?

Code:
switch ($zip)
{
case 23923
case 39124
case 12302:
  echo "case 1";
  break;
case 90210
case 90211
case 90212:
  echo "case 2";
  break;
default:
  echo "all else";
}
 
I am not an extensive php programmer but why not use a switch statement?

Code:
switch ($zip)
{
case 23923
case 39124
case 12302:
  echo "case 1";
  break;
case 90210
case 90211
case 90212:
  echo "case 2";
  break;
default:
  echo "all else";
}

that will not scale well. what if he has thousands of zip codes? At least with an array he could conceivably populate it from a database.
 
that will not scale well. what if he has thousands of zip codes? At least with an array he could conceivably populate it from a database.

True.

One neat trick you can do with php switch statements is put in an expression. So if you can regex the values that may also be an option.

However the array method is probably the better option, was just wondering why no one mentioned the switch statement.
 
Case statements still have static abilities. Here a quick in_array() example:
Code:
<?php
$all	= array("11111",
				"22222",
				"33333",
				"44444");

if(in_array("90210", $all)){
	echo "Zip found";
} else { 
	echo "Zip not found";
}
?>
 
Back
Top