A PHP package providing US states data with static method access.
- PHP 8.3 or higher
composer require llcu/us-statesAll methods are static, so no instantiation is required.
use LLCU\UsStates\States;
// Get all 50 US states (abbreviation => name)
$states = States::all();
// ['AL' => 'Alabama', 'AK' => 'Alaska', ...]// Get state name by abbreviation
$name = States::getName('CA');
// 'California'
// Get abbreviation by state name
$abbr = States::getAbbreviation('California');
// 'CA'// Check if valid state
States::isValidState('CA'); // true
States::isValidState('DC'); // false (DC is a territory)
// Check if valid state
States::isValid('CA'); // true
States::isValid('DC'); // true
States::isValid('XX'); // false// Get all abbreviations
$abbreviations = States::abbreviations();
// ['AL', 'AK', 'AZ', ...]
// Get all names
$names = States::names();
// ['Alabama', 'Alaska', 'Arizona', ...]
// Get count
$count = States::count();
// 50// Search states by partial name
$results = States::search('new');
// ['NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York']// Basic options
$options = States::asSelectOptions();
// With placeholder
$options = States::asSelectOptions(false, 'Select a state...');
// ['' => 'Select a state...', 'AL' => 'Alabama', ...]$random = States::random();
// ['abbreviation' => 'CA', 'name' => 'California']This project uses Pest for testing.