PHP function: get US state name by abbreviation


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 US and two “51st state” (Puerto Rico and Guam).

$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",
    'PR'=>"Puerto Rico",
    'GU'=>"Guam"
);

There are some functions that return the full state name when passed the abbreviation:

function getStateNameByAbbr($abbr)
{

    switch ($abbr)
    {
        case 'AK':
            return 'Alaska';
            break;

        case 'AL':
            return 'Alabama';
            break;

        case 'AR':
            return 'Arkansas';
            break;

        case 'AZ':
            return 'Arizona';
            break;

        case 'CA':
            return 'California';
            break;

        case 'CO':
            return 'Colorado';
            break;

        case 'CT':
            return 'Connecticut';
            break;

        case 'DC':
            return 'District of Columbia';
            break;

        case 'DE':
            return 'Delaware';
            break;

        case 'FL':
            return 'Florida';
            break;

        case 'GA':
            return 'Georgia';
            break;
        
        case 'GU':
            return 'Guam';
            break;

        case 'HI':
            return 'Hawaii';
            break;

        case 'IA':
            return 'Iowa';
            break;

        case 'ID':
            return 'Idaho';
            break;

        case 'IL':
            return 'Illinois';
            break;

        case 'IN':
            return 'Indiana';
            break;

        case 'KS':
            return 'Kansas';
            break;

        case 'KY':
            return 'Kentucky';
            break;

        case 'LA':
            return 'Louisiana';
            break;

        case 'MA':
            return 'Massachusetts';
            break;

        case 'MD':
            return 'Maryland';
            break;

        case 'ME':
            return 'Maine';
            break;

        case 'MI':
            return 'Michigan';
            break;

        case 'MN':
            return 'Minnesota';
            break;

        case 'MO':
            return 'Missouri';
            break;

        case 'MS':
            return 'Mississippi';
            break;

        case 'MT':
            return 'Montana';
            break;

        case 'NC':
            return 'North Carolina';
            break;

        case 'ND':
            return 'North Dakota';
            break;

        case 'NE':
            return 'Nebraska';
            break;

        case 'NH':
            return 'New Hampshire';
            break;

        case 'NJ':
            return 'New Jersey';
            break;

        case 'NM':
            return 'New Mexico';
            break;

        case 'NV':
            return 'Nevada';
            break;

        case 'NY':
            return 'New York';
            break;

        case 'OH':
            return 'Ohio';
            break;

        case 'OK':
            return 'Oklahoma';
            break;

        case 'OR':
            return 'Oregon';
            break;

        case 'PA':
            return 'Pennsylvania';
            break;

        case 'PR':
            return 'Puerto Rico';
            break;

        case 'RI':
            return 'Rhode Island';
            break;

        case 'SC':
            return 'South Carolina';
            break;

        case 'SD':
            return 'South Dakota';
            break;

        case 'TN':
            return 'Tennessee';
            break;

        case 'TX':
            return 'Texas';
            break;

        case 'UT':
            return 'Utah';
            break;

        case 'VA':
            return 'Virginia';
            break;

        case 'VT':
            return 'Vermont';
            break;

        case 'WA':
            return 'Washington';
            break;

        case 'WI':
            return 'Wisconsin';
            break;

        case 'WV':
            return 'West Virginia';
            break;

        case 'WY':
            return 'Wyoming';
            break;

    }
}
function getStateNameByAbbr2($abbr)
{
    if ($abbr == "AK")
    {
        return "Alaska";
    }
    if ($abbr == "AL")
    {
        return "Alabama";
    }
    if ($abbr == "AR")
    {
        return "Arkansas";
    }
    if ($abbr == "AZ")
    {
        return "Arizona";
    }
    if ($abbr == "CA")
    {
        return "California";
    }
    if ($abbr == "CO")
    {
        return "Colorado";
    }
    if ($abbr == "CT")
    {
        return "Connecticut";
    }
    if ($abbr == "DC")
    {
        return "District of Columbia";
    }
    if ($abbr == "DE")
    {
        return "Delaware";
    }
    if ($abbr == "FL")
    {
        return "Florida";
    }
    if ($abbr == "GA")
    {
        return "Georgia";
    }
    if ($abbr == "GU")
    {
        return "Guam";
    }
    if ($abbr == "HI")
    {
        return "Hawaii";
    }
    if ($abbr == "IA")
    {
        return "Iowa";
    }
    if ($abbr == "ID")
    {
        return "Idaho";
    }
    if ($abbr == "IL")
    {
        return "Illinois";
    }
    if ($abbr == "IN")
    {
        return "Indiana";
    }
    if ($abbr == "KS")
    {
        return "Kansas";
    }
    if ($abbr == "KY")
    {
        return "Kentucky";
    }
    if ($abbr == "LA")
    {
        return "Louisiana";
    }
    if ($abbr == "MA")
    {
        return "Massachusetts";
    }
    if ($abbr == "MD")
    {
        return "Maryland";
    }
    if ($abbr == "ME")
    {
        return "Maine";
    }
    if ($abbr == "MI")
    {
        return "Michigan";
    }
    if ($abbr == "MN")
    {
        return "Minnesota";
    }
    if ($abbr == "MO")
    {
        return "Missouri";
    }
    if ($abbr == "MS")
    {
        return "Mississippi";
    }
    if ($abbr == "MT")
    {
        return "Montana";
    }
    if ($abbr == "NC")
    {
        return "North Carolina";
    }
    if ($abbr == "ND")
    {
        return "North Dakota";
    }
    if ($abbr == "NE")
    {
        return "Nebraska";
    }
    if ($abbr == "NH")
    {
        return "New Hampshire";
    }
    if ($abbr == "NJ")
    {
        return "New Jersey";
    }
    if ($abbr == "NM")
    {
        return "New Mexico";
    }
    if ($abbr == "NV")
    {
        return "Nevada";
    }
    if ($abbr == "NY")
    {
        return "New York";
    }
    if ($abbr == "OH")
    {
        return "Ohio";
    }
    if ($abbr == "OK")
    {
        return "Oklahoma";
    }
    if ($abbr == "OR")
    {
        return "Oregon";
    }
    if ($abbr == "PA")
    {
        return "Pennsylvania";
    }
    if ($abbr == "PR")
    {
        return "Puerto Rico";
    }
    if ($abbr == "RI")
    {
        return "Rhode Island";
    }
    if ($abbr == "SC")
    {
        return "South Carolina";
    }
    if ($abbr == "SD")
    {
        return "South Dakota";
    }
    if ($abbr == "TN")
    {
        return "Tennessee";
    }
    if ($abbr == "TX")
    {
        return "Texas";
    }
    if ($abbr == "UT")
    {
        return "Utah";
    }
    if ($abbr == "VA")
    {
        return "Virginia";
    }
    if ($abbr == "VT")
    {
        return "Vermont";
    }
    if ($abbr == "WA")
    {
        return "Washington";
    }
    if ($abbr == "WI")
    {
        return "Wisconsin";
    }
    if ($abbr == "WV")
    {
        return "West Virginia";
    }
    if ($abbr == "WY")
    {
        return "Wyoming";
    }

}

Whats next?

The next thing to do with this would be to just chuck the State details into a database and query them which would be much faster. The advantage about my way is that you can use it when writing code for others or systems that are unknown to you.

Next: PHP function: get US state abbreviation from state name

Leave a Reply