PHPackages                             jabarihunt/country-codes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jabarihunt/country-codes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jabarihunt/country-codes
========================

A simple class that returns country codes (or country name).

v1.0.4(6y ago)05MITPHPPHP &gt;=7.0.0

Since Jan 6Pushed 6y ago1 watchersCompare

[ Source](https://github.com/jabarihunt/country-codes)[ Packagist](https://packagist.org/packages/jabarihunt/country-codes)[ Docs](https://github.com/jabarihunt/country-codes)[ RSS](/packages/jabarihunt-country-codes/feed)WikiDiscussions master Synced yesterday

READMEChangelog (5)DependenciesVersions (5)Used By (0)

Country Code Class
==================

[](#country-code-class)

This is a simple class used to get country codes and/or names. Normally this is something that would be stored and accessed from a database. However, this class will come in handy in situations where you...

1. Are running your application someplace without access to a database
2. Don't have proper permissions to add tables to a database
3. Outside of a countries table, your application doesn't need a database

**Requires PHP 7.0+** (*due to type definitions*)

INSTALLING
----------

[](#installing)

#### Via Composer

[](#via-composer)

Run the following command in the same directory as your composer.json file:

`composer require jabarihunt/country-codes`

#### Via Github

[](#via-github)

1. Clone this repository into a working directory: `git clone git@github.com:jabarihunt/country-codes .`
2. Include the CountryCodes class in your project...

```
require('/path/to/CountryCodes.php')
```

...or if using an auto-loader...

```
 use jabarihunt/CountryCodes;
```

USAGE
-----

[](#usage)

### Available Methods

[](#available-methods)

There are two static methods publicly available in the class...

- **get(** `int $codeType`, `string $code`, `bool $useAPI = FALSE` **)**This function accepts a code type (availabe as class constants) and a corresponding value. The function returns an array containing the country name, 2 letter country code, 3 letter country code, and UN country code.
- **getAll(** `bool $useAPI = FALSE` **)**Returns a multidemintional array containing the country name, 2 letter country code, 3 letter country code, and UN country code for ALL countries.

> ***NOTE:*** The countries data is hard coded into the class as an array. However, the optional parameter (`$useAPI`) will spawn an HTTP request to the [REST Countries API](https://restcountries.eu/) to retrieve live data. It's rare that countries change, but it does happen!

Additionally, there are five publicly available constants that represent class data types:

```
public const CountryCodes::CODE_TYPE_COUNTRY_NAME
public const CountryCodes::CODE_TYPE_NATIVE_NAME
public const CountryCodes::CODE_TYPE_2_LETTER
public const CountryCodes::CODE_TYPE_3_LETTER
public const CountryCodes::CODE_TYPE_UN
```

### Retrieving Data For A Single Country

[](#retrieving-data-for-a-single-country)

Suppose we have a country name, *Haiti* for example, and we want the 3 letter country code. We will use the `get()` method by passing it the type of value (country name in this case) along with the value itself.

```

```

```
/* OUTPUT */

array(5) {
    ["name"]=> string(5) "Haiti"
    ["nativeName"]=> string(6) "Haïti"
    ["2letter"]=> string(2) "HT"
    ["3letter"]=> string(3) "HTI"
    ["un"]=> string(3) "332"
}

```

As you can see, this method returns an array containing the country's official name, native name (*in it's native language*), 2 letter country code, 3 letter country code, and United Nations numeric code. This will be the case regardless if the source is the hard coded class data or from an HTTP request to the [REST Countries API](https://restcountries.eu/). The following would return the exact same output (*note the use of the third optional parameter*):

`CountryCodes::get($countryCodeType, $countryName, TRUE)`

> ***NOTE:*** Acknowledged country names are exactly as in the [REST Countries API](https://restcountries.eu/). Most are straight forward as with "*Haiti*" in the example above. However, some aren't so obvious, such as "*Venezuela (Bolivarian Republic of)*" rather than just "*Venezuela*"!

The class can be used as a reverse lookup if you happened to have a country code and wish to receive a country name (or other data point from the class). The below example would return the exact same output as the one above:

```

```

### Retrieving All Countries

[](#retrieving-all-countries)

There may be instances where you want to retrieve all country codes. The below example will return a multidimensional array of country codes with the same keys as in the example above.

```

```

CONTRIBUTING
------------

[](#contributing)

1. Fork Repository
2. Create a descriptive branch name
3. Make edits to your branch
4. Squash (rebase) your commits
5. Create a pull request

LICENSE
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

2318d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2807764?v=4)[Jabari J. Hunt](/maintainers/jabarihunt)[@jabarihunt](https://github.com/jabarihunt)

---

Top Contributors

[![jabarihunt](https://avatars.githubusercontent.com/u/2807764?v=4)](https://github.com/jabarihunt "jabarihunt (8 commits)")

---

Tags

country codeUN

### Embed Badge

![Health badge](/badges/jabarihunt-country-codes/health.svg)

```
[![Health](https://phpackages.com/badges/jabarihunt-country-codes/health.svg)](https://phpackages.com/packages/jabarihunt-country-codes)
```

###  Alternatives

[tapp/filament-country-code-field

Filament country code field.

2017.6k1](/packages/tapp-filament-country-code-field)[sun/country

Sun Country is the package that helps you to get the country name &amp; dialing code by the country ISO 3166-1 Alpha-2 code.

1016.5k](/packages/sun-country)[elephox/mimey

PHP package for converting file extensions to MIME types and vice versa.

16217.1k5](/packages/elephox-mimey)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
