PHP code example of mutebesi / globalcountries

1. Go to this page and download the library: Download mutebesi/globalcountries library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mutebesi / globalcountries example snippets


use GlobalCountries\Countries;

// Get all countries
$countries = Countries::all();

// Find a specific country by ISO code (Alpha-2, Alpha-3, or Numeric)
$kenya = Countries::find('KE');

echo $kenya->name;         // Kenya
echo $kenya->capital;      // Nairobi
echo $kenya->population;   // 53771300
echo $kenya->flagEmoji();  // 🇰🇪

// 1. One-liner to get all African country names joined by commas
echo Countries::whereContinent('Africa')->sortBy('name')->join(', ');

// 2. Pluck specific fields as an associative array [iso2 => callingCode]
$dialCodes = Countries::whereContinent('Europe')->pluck('callingCode', 'iso2');

// 3. Filter countries by population threshold
$giants = Countries::wherePopulationAbove(100000000); // 100M+

// 4. Guess country from phone and get its currency symbol instantly
echo Countries::guessFromPhone('+254712345678')->currencySymbol(); // KSh

// 5. Group by continent and get the count for each
foreach (Countries::all()->groupBy('continent') as $continent => $group) {
    echo "{$continent}: {$group->count()} countries";
}

// Filter by continent
$africa = Countries::whereContinent('Africa');

// Filter by language (ISO 639-2 or 639-1)
$spanishSpeaking = Countries::whereLanguage('spa');

// Get all EU member states
$eu = Countries::eu();

// Search by name or code (fuzzy search)
$results = Countries::search('United');

// Sorting
$sorted = Countries::all()->sortBy('population');

use Illuminate\Validation\Rule;

$request->validate([
    'country_code' => ['

// Generate practical HTML selects with flags and grouping
foreach (Countries::all()->sortBy('name') as $country) {
    echo "<option value='{$country->iso2}'>{$country->flagEmoji} {$country->name}</option>";
}