PHP United States State List. (Array)
5
This is a state list I made for a website. I kept it just in case i ever needed it again, which i have. I figured it would probably help someone else out.
It includes all states in the U.S.
It includes all states in the U.S.
$state_list = array('AL'=>"Alabama",
'AK'=>"Alaska",
'AZ'=>"Arizona",
'AR'=>"Arkansas",
'CA'=>"California",
'CO'=>"Colorado",
'CT'=>"Connecticut",
'DE'=>"Delaware",
'DC'=>"District Of Columbia",
'FL'=>"Florida",
'GA'=>"Georgia",
'HI'=>"Hawaii",
'ID'=>"Idaho",
'IL'=>"Illinois",
'IN'=>"Indiana",
'IA'=>"Iowa",
'KS'=>"Kansas",
'KY'=>"Kentucky",
'LA'=>"Louisiana",
'ME'=>"Maine",
'MD'=>"Maryland",
'MA'=>"Massachusetts",
'MI'=>"Michigan",
'MN'=>"Minnesota",
'MS'=>"Mississippi",
'MO'=>"Missouri",
'MT'=>"Montana",
'NE'=>"Nebraska",
'NV'=>"Nevada",
'NH'=>"New Hampshire",
'NJ'=>"New Jersey",
'NM'=>"New Mexico",
'NY'=>"New York",
'NC'=>"North Carolina",
'ND'=>"North Dakota",
'OH'=>"Ohio",
'OK'=>"Oklahoma",
'OR'=>"Oregon",
'PA'=>"Pennsylvania",
'RI'=>"Rhode Island",
'SC'=>"South Carolina",
'SD'=>"South Dakota",
'TN'=>"Tennessee",
'TX'=>"Texas",
'UT'=>"Utah",
'VT'=>"Vermont",
'VA'=>"Virginia",
'WA'=>"Washington",
'WV'=>"West Virginia",
'WI'=>"Wisconsin",
'WY'=>"Wyoming");






// I just threw this together real quick to give a general idea of how it could be used
define('StatesText',1);
define('StatesSelect',2);
define('StatesRadio',3);
// insert the array of states here... i'm not going to repost it for space sake
function displayStates($format = StatesText){
global $states;
foreach ($states as $abbr => $stateName){
switch ($format){
case StatesText: // insert state as text only not in a form input
echo "$abbr - $stateName<br>";
break;
case StatesSelect: // insert states as select options
echo "<option value=\"$abbr\">$stateName</option>";
break;
case StatesRadio: // insert states as radio input type
echo "<input type=\"radio\" name=\"states\" value=\"$abbr\"> $stateName";
break;
}
}
}
I know there have been plenty of times when I need to create a state/province drop-down for a form (although not with PHP).
--
Chris Gmyr
www.chrisgmyr.com
www.syracusecs.com
www.syracusebands.net
Here is a more complete tutorial on how to get the box working as well.