[英]Php switch statement - how to manage repeated cases?
我正在構建一個站點,用戶可以在其中從應該顯示內容的三個區域中進行選擇。 我有以下地區:
我有以下switch語句:
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;
}
我在切換代碼的結構方面遇到了麻煩,在這一部分中,我必須包括所有國家(地區0)。 似乎我無法重復在先前的案例聲明中聲明的國家(很明顯,我知道),但是我找不到正確的方法。
有什么方法可以重組我的代碼,以便有一個包含所有國家/地區的案例說明? 如果不可能通過開關進行操作,那么你們還有其他建議嗎?
謝謝!
選項A)對情況進行硬編碼(壞主意)
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";
選項B)使用外部.ini文件
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";
如@KenWhite所述,按區域分組,然后對照區域進行檢查。
您的代碼可能看起來像這樣:
<?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.
如果我理解這個問題,則您有兩個互斥的代碼路徑,而一個包含一個。 我將放棄使用switch
和使用集(即PHP數組)。
$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...
}
更好的是(如果可能的話)將代碼路徑移至它們自己的函數:
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...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.