我有一个小脚本来根据用户的来源格式化价格。
我现在的问题是什么是更好的性能明智?
function FormatPrice($Price) {
$Locale = $this->Locale;
switch ($Locale) {
case "en-GB":
case "en-IE":
case "he-IL":
case "mt-MT":
case "zh-CN":
return number_format($Price, 2, '.', ',');
default:
return number_format($Price, 2, ',', '.');
}
}
或
function FormatPrice($Price) {
$Locale = $this->Locale;
if ($Locale === "en-GB" || $Locale === "en-IE" || $Locale === "he-IL" || $Locale === "mt-MT" || $Locale === "zh-CN") {
return number_format($Price, 2, '.', ',');
} else {
return number_format($Price, 2, ',', '.');
}
}
使用下面的链接了解更多信息似乎编译器在优化switch语句方面比if语句做得更好。Case vs If Else If:哪个更有效?