简体   繁体   English

计算器在PHP中使用开关盒

[英]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. 我已经在PHP使用if语句创建了一个计算器,并且能够获得所需的输出。 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 + & - . 1)您已给提交值+-
2) Whenever you submit your form it takes the value + & - in POST value. 2)每当您提交表单时,它的POST值都为+-
3) In switch case you mentioned cases : addition & sub , where your post value having + & - . 3)在切换情况下,您提到的情况是: additionsub ,其中您的帖子值具有+- Which not satisfying any cases. 这不满足任何情况。
4) Just replace your + & - with addition & sub respectively in your form input value like this 4)只需更换您的+-additionsub分别在类似这样的表单输入值

<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. 1)您已声明函数,但从未调用过它。

2) Variable names issue. 2)变量名称问题。

3) Submit button value and switch case never match etc... 3)提交按钮值和开关盒永不匹配等...

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. 它们不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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