Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
Can we use Arithmetic Operators ( + - * / ) in Switch Statement in PHP?

I was trying to make a calculator and wanted to use Arithmetic Operators in the switch Statement but i am not able to.

Can someone help me out.

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>

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

<!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>

  1. Put break after every case
  2. These was a naming error in the code with the names of the variables. eg num1 and num2. Name them correctly
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda