繁体   English   中英

计算器在PHP中使用开关盒

[英]Calculator using switch case in PHP

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

这是我的代码:

<!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)您已给提交值+-
2)每当您提交表单时,它的POST值都为+-
3)在切换情况下,您提到的情况是: additionsub ,其中您的帖子值具有+- 这不满足任何情况。
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>

您的代码中有很多错误。 喜欢

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>

您将“ +”和“-”传递到开关,大小写为“加法”和“子”。 它们不匹配。

暂无
暂无

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

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