Hi everybody i am trying to make a calculator using "switch" but the browser always told me that one of my variable did not exist i realy need some help and thank u

<html>
<head>
<title>
FORMULAIRE CALCULATRICE
</title>
</head>
<body>
<pre>
<form action="calculatrice.php" method=" POST">
ENTRER VOTRE 1<sup>ere</sup>NOMBRE :<input type="text"  name="nbr1"   size="4">
ENTRER VOTRE 2<sup>eme</sup>NOMBRE :<input type="text"  name="nbr2" size="4">

CHOIX D'OPERATION : <select name="operation" >
<option value="+">+</option>
<option VALUE="-"> -</option>
<option value="*">*</option>
<option value="/"> /</option>
<option value="%"> %</option>
</select>


<input type ="submit"  value="calculer"/>
</pre>
</form>
</body>
</html>
<?php
echo"h1";
 if ( isset($_POST['nbr1']) && isset ($_POST['nbr2'])  && isset($_POST['operation'] )){

   if ( !empty ( $_POST['nbr1'] ) && !empty( $_POST['nbr2'] ) && !empty( $_POST['operation'] )){


   $n1=$_POST['nbr1'];
   $n2=$_POST['nbr2'];
   $opr=$_POST['operation'];


   switch($opr) {

   case'+': $c=$n1+$n2 ;

            break;
   case'-': $c=$n1-$n2 ;

            break;
   case'*': $c=$n1*$n2 ;

            break;

   case'/': if($n2!=0){
                $c=$n1/$n2 ;
                }
                else echo"Erreur, Division impossible!";
				break;


case'%':if($n2 != 0){ 
            $c=$n1%$n2 ;
            }
            else echo"Erreur, Division impossible  !";
			break;

			default :ECHO" SVP choisissez un operation a effectué";

 echo "the result is $c "
   }
   }
   else echo" one of ur variabl is  vide"; }
   else echo" one of ur variable did not exist ";
?>

    you are doing this in several places:

    if($n2!=0){
         $c=$n1/$n2 ;
     }
         else echo"Erreur, Division impossible!";

    in other words mixing up curly bracket usage

    just follow this style in every place you use if or if and else:

    if($n2!=0){
         $c=$n1/$n2 ;
    } else {
         echo"Erreur, Division impossible!";
    }
    

      sorry - another error on my part -didn't realise you could mix and match
      however I do think the mix and match approach is very bad practice and makes the logic difficult to scan

        Write a Reply...