For example the 50 united states. Right now I use the 50 states in only a single function:
function getStateInput() {
$states = array('AL' => 'Alabama', 'AK' => 'Alaska',
'AZ' => 'Arizona', etc);
//use $states
}
Alternatively you could define $states in its own states.php and then use it like this:
function getStateInput() {
include('states.php');
//use $states
}
But for some reason that scares me because I am using an include to define a local function variable. Another way would be to assign a states array to a superglobal in a states.php (for example I could use $_ENV['states']). Then I could go:
include('states.php');
function getStateInput() {
//use $_ENV['states']
}
Is one of those best or is there another better way?
The common way is to separate concerns. You can eg. have your own class with address-related methods. This class would be the natural place to store information about possible states, like that:
class Address {
static STATES = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
// ...
);
}
// and then you can use it somewhere like that:
echo Address::$STATES['AL'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With