-3

this function only adds numbers and I can not figure why ?

function calculate($num1 , $num2 , $name = "+" )
{
 
  switch ($name)
  {
    case "+" || "add" || "a";
    return $num1 + $num2 ."<br>";
    break;

    case "-" || "subtract" || "s";
    return $num1 - $num2 ."<br>";
    break;

    case "*" || "multiply" || "m";
    return $num1 * $num2 ."<br>";
    break;

    default:
    "";
  }
     
}

I also tried if statement and didnt work.

1 Answers1

0

Your syntax is not correct, do as follows:

switch ($name) {
        case "+" :
        case "add" :
        case "a":
            return $num1 + $num2 . "<br>";

        case "-" :
        case "subtract" :
        case "s":
            return $num1 - $num2 . "<br>";

        case "*" :
        case "multiply" :
        case "m":
            return $num1 * $num2 . "<br>";

        default:
            "";
    }

By the way, you don't need to break if you return something.

More information :

Switch case multiple values

Switch Documentation