通常我們在顯示某些資料時, 會依照變數值不同而顯示不同的資料,
而通常都會用 if () else {} 或是 switch () 來.
舉例:
<tr>
<td>性別</td>
<td><?
if ($gender == "M") {
echo "男";
} else if ($gender == "F") {
echo "女";
}
// 或是用 Switch()
?></td>
</tr>
在這種情況, 可以用以下的方式顯示:
<?php
$gender_option['M'] = '男';
$gender_option['F'] = '女';
?>
<tr>
<td>性別</td>
<td><?= $gender_option[$gender] ?></td>
</tr>
這樣看起來 code 感覺就比較清楚.
但其實效率來說, 並沒有比較快.
除了性別以外, 也可以用在其他地方, 例如: 興趣, 城市等.
$interest_options[1] = '羽毛球';
$interest_options[2] = '籃球';
$interest_options[3] = '排球';
$interest_options[4] = '游泳';
....