使用PHP中的开关案例的计算器

时间:2018-03-21 05:44:22

标签: php html

我使用if中的PHP语句创建了一个计算器,我能够获得所需的输出。 我卡在开关盒里。我没有得到我想要的输出。

这是我的代码:

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

4 个答案:

答案 0 :(得分:1)

1)您已提交提交值+&amp; -
2)每当您提交表单时,其值为+&amp; -POST
3)在转换案例中,您提到了案例:addition&amp; sub,其中您的帖子值为+&amp; -。这不满足任何情况。
4)只需更换+&amp; -addition&amp; sub分别在您的表单中输入值

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


答案 1 :(得分:0)

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

答案 2 :(得分:-1)

你的代码中有很多错误。像

1)你已经声明了函数但从未调用它。

2)变量名称问题。

3)提交按钮值并切换案例永不匹配等...

您需要更改以下代码:

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

答案 3 :(得分:-1)

您将“+”和“ - ”传递给开关,案例是“添加”和“子”。 它们不匹配。