Change elseif code to switch case

0

I'm having trouble changing a code I made in else if to switch case . I'm not getting the code to work, it gets values and operation by $_GET and does the selected operation:

I leave the code here:

if((!is_numeric($num1) || $num1 == '' || $num1 == '0') && $operator == '/') 
    echo "ERRO: Numero 1 inserido e' zero";
else if((!is_numeric($num2) || $num2 == '' || $num2 == '0') && $operator == '/') 
    echo "ERRO: Numero 2 inserido e' zero";
else {
   //se a divisao for feita por zero, da mensagem de erro informando que o valor inserido foi zero
    if( !empty($operator) ) {
        if($operator == '+')
            $result = $num1 + $num2;
        else if($operator == '-')
            $result = $num1 - $num2;
        else if($operator == '*')
            $result = $num1*$num2;
        else
            $result = $num1/$num2;
        echo "O resultado é: ".$result;
   }
}
    
asked by anonymous 07.07.2016 / 15:16

2 answers

4

I would need to improve a lot, but basically it would be this:

if (!is_numeric($num1) || !is_numeric($num2) {
    echo "ERRO: as entradas precisam ser numéricas";
    //encerra de alguma forma, dpende do código.
}
$num1 = intval($num1); //pode  ser um floatval
$num2 = intval($num2);
switch ($operator) {
    case '+':
        $result = $num1 + $num2;
        break;
    case '-':
        $result = $num1 - $num2;
        break;
    case '*':
        $result = $num1 * $num2;
        break;
    case '/':
        if ($num2 == 0) {
            echo "ERRO: Não pode fazer divisão por zero";
            break;
        }
        $result = $num1 / $num2;
        break;
    default:
        //talvez mereça alguma mensagem de erro se não for nenhum operador válido
}
echo "O resultado é: " . $result;

Do not convert to switch what if works best, do so only with the choice of operators.

Do not forget to convert the values that are like text to numeric to make the account properly.

The test if it is numeric is a bit naive.

The test if the value of the divisor is 0 only needs to be done if the operator is a division, so put it there. And the dividend can be 0.

There are several other things that could be done better.

Perhaps the difficulty was because it was not separating each responsibility. It's one thing to check if the data is within the minimum standards to do something useful, something else and select what to do. I do not think it's worth checking the conditional value separately (case of division by 0).

You need to see whether you want only integers or also decimal values. It has a number of other decisions to make a good code to use. If it was a basic exercise then it would have been better to at least try to do what you want.

    
07.07.2016 / 15:36
2

You can try this:

    $num1 = $_GET['num1']; 
    $operator = $_GET['operator']; 
    $num2 = $_GET['num2']; 

    switch (true) {
    case !is_numeric($num1) && $operator == '/':
    case $num1 == '' && $operator == '/':
    case $num1 == '0' && $operator == '/':
        echo "ERRO: Numero 1 inserido e' zero";
        break;

    case !is_numeric($num2) && $operator == '/':
    case $num2 == '' && $operator == '/':
    case $num2 == '0' && $operator == '/':
        echo "ERRO: Numero 2 inserido e' zero";
        break;
    default:
        switch ($operator) {
            case '+':
                $result = $num1 + $num2;
                break;
            case '-':
                $result = $num1 - $num2;
                break;
            case '*':
                $result = $num1 * $num2;
                break;
            case '/':
                $result = $num1 / $num2;
                break;
        }
        echo "O resultado é: ".$result;
    }
    
07.07.2016 / 15:35