Frequently while programming, I find myself in need of a complete list of the States, their abbreviations and sometimes even Countries. Usually I end up prowling Google trying to find some sort of list that I can then import in to a database. Each time I did it I kept wondering why I hadn’t created some script for that yet.
I know it’s more efficient to make use of a database with tables for States and Countries, however, in the event that I don’t need one or am just doing some sort of one off solution, I’ve created a couple functions and two arrays to help myself, and maybe you guys out.
Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | $usStateList = array( "AL" => "Alabama", "AK" => "Alaska", "AS" => "American Samoa", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "DC" => "District of Columbia", "FL" => "Florida", "GA" => "Georgia", "GU" => "Guam", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MH" => "Marshall Islands", "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", "MP" => "Northern Mariana Islands", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "PR" => "Puerto Rico", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VI" => "Virgin Islands", "VA" => "Virginia", "WA" => "Washington", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming" ); $countryList = array( "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, The Democratic Republic of The", "Cook Islands", "Costa Rica", "Cote D'ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-bissau", "Guyana", "Haiti", "Heard Island and Mcdonald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", "Saint Vincent and The Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and The South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Timor-leste", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.", "Wallis and Futuna", "Western Sahara", "Yemen", "Zambia", "Zimbabwe" ); |
Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # Generate Country Select List function generateCountrySelect($cName, $secondaryParams="", $cType=0, $echoIt = true, $defaultVal = '') { global $countryList; $theOutput = ''; // Build the Select Menu $theOutput .= '<select name="'.$cName.'" id="'.$cName.'" '.$secondaryParams.'>'; switch($cType) { // Alphabetical order case 0: $theOutput .= '<option value="">Select a Country</option>'; foreach($countryList as $cCountry) { $theOutput .= '<option value="'.$cCountry.'">'.$cCountry.'</option>'; } break; // Most used countries first case 1: $theOutput .= '<option value="">Select a Country</option>'; $theOutput .= '<optgroup label="Frequently Selected">'; $theOutput .= '<option value="United States">United States</option>'; $theOutput .= '<option value="United Kingdom">United Kingdom</option>'; $theOutput .= '</optgroup>'; $theOutput .= '<optgroup label="Countries">'; foreach($countryList as $cCountry) { $theOutput .= '<option value="'.$cCountry.'">'.$cCountry.'</option>'; } $theOutput .= '</optgroup>'; break; } $theOutput .= '</select>'; if ($echoIt) { echo $theOutput; } else { return $theOutput; } } |
This function is for generating a dropdown of the Countries. Here’s a list of the parameters and what you can achieve by changing or setting them:
- $cName gets set to the name and the id of the dropdown.
- $secondaryParams is left blank by default, but you can add inline classes, onclick, onchange or any other parameters this way.
- $cType is set to 0 by default, which will just display all the countries in alphabetical order. You can set it to 1 to show the most used countries (US and the UK in this case). Feel free to modify it to suit your needs.
- $echoIt is true by default and will print the results directly to screen. Set this to false if you’d like to return the output to a variable instead.
- $defaultVal is there if I ever get around to giving the countries shorter names (which would then be used as the value of the option) and used in the logic of selecting a default value. I left it in just in case anyone wants to modify this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # Generate State Select List function generateStateSelect($cName, $secondaryParams="", $echoIt = true, $defaultVal = '') { global $usStateList; $theOutput = ''; // Build the Select Menu $theOutput .= '<select name="'.$cName.'" id="'.$cName.'" '.$secondaryParams.'>'; $theOutput .= '<option value="">Select a State</option>'; foreach($usStateList as $cAbb => $cState) { $theOutput .= '<option value="'.$cAbb.'"'; // Default select if one is set if (isset($defaultVal) && $defaultVal != '') { if ($defaultVal == $cAbb) { $theOutput .= 'selected="selected"'; } } $theOutput .= '>'.$cState.'</option>'; } $theOutput .= '</select>'; if ($echoIt) { echo $theOutput; } else { return $theOutput; } } |
This function is for generating a dropdown of the States. Here’s a list of the parameters and what you can achieve by changing or setting them:
- $cName gets set to the name and the id of the dropdown.
- $secondaryParams is left blank by default, but you can add inline classes, onclick, onchange or any other parameters this way.
- $echoIt is true by default and will print the results directly to screen. Set this to false if you’d like to return the output to a variable instead.
- $defaultVal is used to determine the default State to select based on the shortname passed in this parameter.
Feel free to use, modify or hack and slash these functions in any way you see fit. If you end up using them, please let me know in the comments — it’s always nice to know I’ve helped at least a little.

Wham bam thank you, ma’am, my questions are ansrewed!