我们有变量$country
,它可以给~50个不同的值。PHP if else语句
和变量$id
。
我们应该做的是给价值$id
对应$country
值,如:
if ($country = 'USA') { $id = 'usa_type'; }
else if ($country = 'France') { $id = 'france_type'; }
else if ($country = 'German') { $id = 'german_type'; }
else if ($country = 'Spain') { $id = 'spain_type'; }
...
...
...
else if ($country = 'Urugway') { $id = 'urugway_type'; }
else { $id = 'undefined'; }
else if
语句重复,每次和其他数据是典型的。
有没有办法缩短这段代码?
像:
[france]:'france_type;
[england]:'england_type;
...
[else]:'undefined'
感谢。
为什么这很重要?如果较短的代码难以理解,并且如此复杂,则有可能发生错误,因此有时需要更长的代码是值得的。 – James 2010-08-01 09:03:49