Estaba tratando de hacer una calculadora y quería usar operadores aritméticos en la declaración de cambio, pero no puedo.
Alguien me puede ayudar.
Error:
Warning: Undefined array key "num2" in on line 16 220 Fatal error: Uncaught DivisionByZeroError: Division by zero in 27 Stack trace: #0 {main} thrown on line 27 <!DOCTYPE html> <html> <body> <form action="php.php" method="post"> Number 1: <input type="number" name="num1"><br> Operator: <input type="text" name="op"><br> Number 3: <input type="number" name="num1"><br><br> <input type="submit"> </form> <?php $num1 = $_POST["num1"]; $op = $_POST["op"]; $num2 = $_POST["num2"]; switch($op) { case "+": echo $num1 + $num2; case "-": echo $num1 - $num2; case "*": echo $num1 * $num2; case "/": echo $num1 / $num2; } ?> </body> </html> <!DOCTYPE html> <html> <body> <form action="php.php" method="GET"> Number 1: <input type="number" name="num1"><br> Operator: <input type="text" name="op"><br> Number 3: <input type="number" name="num2"><br><br> <input type="submit"> </form> <?php $num1 = $_GET['num1']; $op = $_GET['op']; $num2 = $_GET['num2']; switch($op) { case '+': echo $num1 + $num2; break; case '-': echo $num1 - $num2; break; case '*': echo $num1 * $num2; break; case '/': echo $num1 / $num2; break; } ?> </body> </html>