-1

I have a small script to format the prices depending on the origin of the user.
My question now is what is better performance wise?

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, ',', '.');

        }
    }

or

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, ',', '.');
        }
    }
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Arjuna Wenzel
  • 142
  • 1
  • 10
  • As it seems there is no need for all the conditions to be checked. Just reverse the conditions. Check for `$Locale === "zh-CN"` – Sougata Bose Aug 03 '15 at 08:41
  • 1
    Both `switch` case and `if` condition doesn't make any difference at all but if you multiple or condition then you can use `switch` as its better in readable ways and for more you can get an answer over [which-is-faster-and-better-switch-case-or-if-else-if](http://stackoverflow.com/questions/10773047/which-is-faster-and-better-switch-case-or-if-else-if) post – Narendrasingh Sisodia Aug 03 '15 at 08:42

1 Answers1

1

Use the below link to understand more It seems that the compiler is better in optimizing a switch-statement than an if-statement. Case vs If Else If: Which is more efficient?

Community
  • 1
  • 1
Ankur Tiwari
  • 2,762
  • 2
  • 23
  • 40