简体   繁体   中英

Php switch statement - how to manage repeated cases?

I'm building a site in which the user should be able to select from three regions where the content should be visible. I have the following regions:

  • Region 1: Argentina, Uruguay, Paraguay, Chile, Venezuela, Peru
  • Region 2: Mexico, Colombia, Ecuador, Brazil, Belize, Costa Rica, El Salvador, Guatemala, Honduras, Nicaragua, Panama
  • Region 0: It should include all of the countries above

I have the following switch statement:

switch ( $countryCode ) {
    case 'AR':
    case 'UY':
    case 'PY':
    case 'CL':
    case 'VE':
    case 'PR':
        // code to execute
        break;

    case 'MX':
    case 'CO':
    case 'EC':
    case 'BR':
    case 'BZ':
    case 'CR':
    case 'SV':
    case 'GT':
    case 'HN':
    case 'NI':
    case 'PA':
        // code to execute
        break;

    case 'AR':
    case 'UY':
    case 'PY':
    case 'CL':
    case 'VE':
    case 'PR':
    case 'MX':
    case 'CO':
    case 'EC':
    case 'BR':
    case 'BZ':
    case 'CR':
    case 'SV':
    case 'GT':
    case 'HN':
    case 'NI':
    case 'PA':
        // code to execute
        break;

    default:
        // code to execute
        break;
}

I'm having trouble with the structure of my switch code, in the part where I have to include all of the countries (Region 0). It seems that I can't repeat countries that have been declared on previous case statements (it's obvious I know), but I can't figure out the right way to do it.

Is there any way to restructure my code so there would be a case statement that includes all of the countries? If it's not posible to do it with a switch, you guys have any other suggestion?

Thank you!

Options A) hard code your scenario (bad idea)

function get_region($country_code){

    switch ( $country_code ) {
        case 'AR':
        case 'UY':
        case 'PY':
        case 'CL':
        case 'VE':
        case 'PR':
            return 1;
            break;

        case 'MX':
        case 'CO':
        case 'EC':
        case 'BR':
        case 'BZ':
        case 'CR':
        case 'SV':
        case 'GT':
        case 'HN':
        case 'NI':
        case 'PA':
            return 2;
            break;

        default:
            return 0;
            break;
    }
}

echo get_region("AR")."\n";
echo get_region("MX")."\n";

Option B) use external .ini files

regions.ini

; region 1
[1]
country[] = "AR"
country[] = "UY"
country[] = "PY"
country[] = "CL"
country[] = "VE"
country[] = "PE"

; region 2
[2]
country[] = "MX"
country[] = "CO"
country[] = "EC"
country[] = "BR"
country[] = "BZ"
country[] = "CR"
country[] = "SV"
country[] = "GT"
country[] = "HN"
country[] = "NI"
country[] = "PA"

function.php:

function get_region($country_code){

    $region = parse_ini_file("regions.ini", true);

    if (in_array($country_code,$region[1]['country'], false)){
        return 1;
    } elseif (in_array($country_code,$region[2]['country'], false)){
        return 2;
    } else {
        return 0;
    }
}

echo get_region("AR")."\n";
echo get_region("MX")."\n";

As @KenWhite mentioned, group by region, then check against regions.

Your code could look something like this:

<?php

$regions = [false, false, false];

switch ( $countryCode ) {
    case 'AR':
    case 'UY':
    case 'PY':
    case 'CL':
    case 'VE':
    case 'PR':
        $regions[0] = $regions[1] = true;
        break;

    case 'MX':
    case 'CO':
    case 'EC':
    case 'BR':
    case 'BZ':
    case 'CR':
    case 'SV':
    case 'GT':
    case 'HN':
    case 'NI':
    case 'PA':
        $regions[0] = $regions[2] = true;
        break;

    default:
        break;
}

if($regions[0]) {
    //some code here
}
// etc.

If I understand the question, you have two mutually exclusive code paths and one inclusive one. I would move away from using switch and use sets (ie PHP arrays).

$region1 = array_combine(['AR','UY','PY','CL','VE','PR'],true);
$region2 = array_combine(['MX','CO','EC','BR','BZ','CR','SV',
                          'GT','HN','NI','PA',],true);

if (isset($region1[$countryCode])) {
    // Region 1 specific...

}
else if (isset($region2[$countryCode])) {
    // Region 2 specific...

}

if (isset($region1[$countryCode],$region2[$countryCode])) {
    // Region 0 specific...

}
else {
    // Default case...

}

Better yet (if possible) move the code paths to their own functions:

function region1() { }
function region2() { }
function region0() { }

$region1 = array_combine(['AR','UY','PY','CL','VE','PR'],true);
$region2 = array_combine(['MX','CO','EC','BR','BZ','CR','SV',
                          'GT','HN','NI','PA',],true);

if (isset($region1[$countryCode])) {
    region1();
    region0();
}
else if (isset($region2[$countryCode])) {
    region2();
    region0();
}
else {
    // Default case...

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM