简体   繁体   中英

Calculator using switch case in PHP

I have created a calculator using if statement in PHP and I was able to get the output i needed. I am stuck with switch case. I'm not getting the output I want.

This is my code:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>

1) You have given submit value + & - .
2) Whenever you submit your form it takes the value + & - in POST value.
3) In switch case you mentioned cases : addition & sub , where your post value having + & - . Which not satisfying any cases.
4) Just replace your + & - with addition & sub respectively in your form input value like this

<input type="submit" name="submit" value="addition">
<input type="submit" name="submit" value="sub">


<!DOCTYPE HTML>
<html>
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Untitled Document</title>
   <?php 

      $num1 = 0;
      $num2 = 0;
      $calc = 0;

      if (isset($_POST['submit']))
      {
        $num1 = $_POST['n1'];
        $num2 = $_POST['n2'];
        $calc =  calculate($num1, $num2, $_POST['submit']);
      }

     function calculate($num1,$num2,$op) 
     {
       $calc = 0;

       switch ($op) 
       {
          case '+':   
                    $calc = $num1 + $num2;
                    break;
          case '-':
                    $calc = $num1 - $num2;
          break;
       }

       return $calc;

    }
  ?>
 </head>

 <body>

    <form action="" method="post">
       NO 1 : <input  name='n1' value="<?php echo $num1;?>">
       <br><br>
       NO 2: <input  name='n2' value="<?php echo $num2;?>"><br><br>
       total: <input name="res" value="<?php echo $calc;?>"><br><br>
       <input type="submit" name="submit" value="+">
       <input type="submit" name="submit" value="-">

   </form>
 </body>
</html>

You have lot of bugs in your code. like

1) You have declared function but never call it.

2) Variable names issue.

3) Submit button value and switch case never match etc...

You need to change your code as below:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2){
  $calc = "";
  switch ($_POST['submit']) {

    case 'addition':
      $calc = $num1 + $num2;
      break;

    case 'sub':
      $calc = $num1 - $num2;
      break;
  }
return $calc;
}
$calc = calculate($num1,$num2);
}
?>
</head>

<body>

  <form action="" method="post">
    NO 1 : <input  name='n1' value="<?php echo $num1;?>">
    <br><br>
    NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
    total: <input type="res" value="<?php echo $calc;?>"><br><br>
    <input type="submit" name="submit" value="addition">
    <input type="submit" name="submit" value="sub">

    </form>
</body>
</html>

You are passing "+" and "-" into the switch and the case is "addition" and "sub". They are not matching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM