problem with php switch

1

Hi, I'm new to programming. I'm trying to do basic operations through a radio button, but I do not work on addition or subtraction. I do not understand why.

<form method="POST" action="resultado.php">
  <table>
    <tr>
      <th>Numreo 1</th>
      <td>
        <input type="text" name="n1"></input>
      </td>
    </tr>
    <tr>
      <th>Numero 2</th>
      <td>
        <input type="text" name="n2"></input>
      </td>
    </tr>
    <tr>
      <td>
        <input type="radio" name="operacion" value="suma">Suma</input>
        <input type="radio" name="operacion" value="resta">Resta</input>
        <input type="radio" name="operacion" value="producto">Multiplicacion</input>
      </td>
      <td>
        <input type="radio" name="operacion" value="division">Division</input>
        <input type="radio" name="operacion" value="potencia">Potencia</input>
        <input type="radio" name="operacion" value="modulo">Modulo</input>
      </td>
    </tr>
    <tr>
      <td></td>
      <td>
        <input type="submit" name="enviar" value="ENVIAR"></input>
      </td>
    </tr>
  </table>
</form>
//otra pagina
<?php 
		$n1 = $_POST['n1'];

		$n2 = $_POST['n2'];


		switch ($_POST['operacion']) {
			case 'suma':
					echo "el resultado es ".$n1+$n2;		
				break;
			case 'resta':
					echo "el resultado es ".$n1-$n2;
				break;
			case 'producto':
					echo "el resultado es ".$n1*$n2;
					break;
			case 'division':
					echo "el resultado es ".$n1/$n2;
						break;
			case 'modulo':
					echo "el resultado es ".$n1%$n2;	
						break;
			case 'potencia':
					echo "el resultado es ".pow($n1, $n2);						
						break;								
			default:
				# code...
				break;
		}
		 ?>
    
asked by Jhonatan Valencia 25.04.2016 в 05:46
source

2 answers

1

You should check, as a minimum, that the values arrive before trying to operate with them. For example:

if ( $n1 != "" && $n2 != "" ) {
  swich ...

Then, always put the mathematical operations in parentheses when you are concatenating with strings to avoid problems and make the code clearer, or better put the result of the operations in a new variable previously and then concatenate it. For example:

switch ($_POST['operacion']) {
    case 'suma':
        $res = $n1 + $n2;
        echo "el resultado es " . $res;      
        break;
    case 'resta':
        $res = $n1 - $n2;
        echo "el resultado es " . $res;  
        break;

When problems arise, simplify and trace / debug.

    
answered by 30.10.2016 / 09:53
source
3

The problem is in the precedence of operators : the concatenation operator ( . ) has the same precedence as the operators of addition ( + ) and subtraction ( - ) and therefore you do not get the result you expected.

Put the operations in parentheses so that these precedence problems do not exist , and that will solve the problem:

    switch ($_POST['operacion']) {
        case 'suma':
                echo "el resultado es ".($n1+$n2);      
            break;
        case 'resta':
                echo "el resultado es ".($n1-$n2);
            break;
        case 'producto':
                echo "el resultado es ".($n1*$n2);
                break;
        case 'division':
                echo "el resultado es ".($n1/$n2);
                    break;
        case 'modulo':
                echo "el resultado es ".($n1%$n2);  
                    break;
        case 'potencia':
                echo "el resultado es ".pow($n1, $n2);                      
                    break;                              
        default:
            # code...
            break;
    
answered by 25.04.2016 в 05:56