【问题标题】:Simple PHP calculator简单的 PHP 计算器
【发布时间】:2014-02-22 02:12:37
【问题描述】:

我正在创建一个基本的 PHP 计算器,它允许您输入两个值并选择您的运算符,然后显示答案。一切正常,只是没有向浏览器输出答案。

以下是我的 html 和 PHP 文件的代码:

<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="post" attribute="post" action="disp_form.php">
<p>First Value:<br/>
<input type="text" id="first" name="first"></p>
<p>Second Value:<br/>
<input type="text" id="second" name="second"></p>
<input type="radio" name="group1" id="add" value="add" checked="true"><p>+</p><br/>
<input type="radio" name="group1" id="subtract" value="subtract"><p>-</p><br/>
<input type="radio" name="group1" id="times" value="times"><p>x</p><br/>
<input type="radio" name="group1" id="divide" value="divide"><p>/</p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>

PHP 文件:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is: 
<?php
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p> 
</body>
</html>

【问题讨论】:

    标签: php calculator


    【解决方案1】:

    你需要分配 $first 和 $second

    $first = $_POST['first'];
    $second= $_POST['second'];
    

    另外,正如 Travesty3 所说,您需要在引号之外进行算术运算:

    echo $first + $second;
    

    【讨论】:

    • 您还需要在引号之外进行计算。否则,您将只输出两个数字,并在它们之间使用运算符,而不是实际进行计算。并将addsubtract 等放在引号中。
    【解决方案2】:

    您需要以相同的方式获取值以获取计算器操作,如下所示:

    <?php
    if($_POST['group1'] == add) {
    echo "$_POST['first']+ $_POST['second'];
    }
    ... and so on
    ?>
    

    或者,为了更容易,只需这样做:

        <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Answer</title>
    </head>
    <body>
    <p>The answer is: 
    <?php
    $first = $_POST['first'];
    $second= $_POST['second'];
    
    if($_POST['group1'] == add) {
    echo "$first + $second";
    }
    else if($_POST['group1'] == subtract) {
    echo "$first - $second";
    }
    else if($_POST['group1'] == times) {
    echo "$first * $second";
    }
    else($_POST['group1'] == divide) {
    echo "$first / $second";
    }
    ?>
    </p> 
    </body>
    </html>
    

    【讨论】:

      【解决方案3】:
      $first = doubleval($_POST['first']);
      $second = doubleval($_POST['second']);
      
      if($_POST['group1'] == 'add') {
          echo "$first + $second = ".($first + $second);
      }
      
      // etc
      

      【讨论】:

        【解决方案4】:

        我个人会做一个切换而不是所有这些 if, else if, else

        $first = $_POST['first'] + 0;//a small "hack" to make sure its an int but allow negs!!
        $second= $_POST['second'] + 0;
        $operator = $_POST["group1"];
        switch($operator)
        {
            case "add"
            echo "Answer is: " .$first + $second;
            break; 
            case "subtract"
            echo "Answer is: " .$first - $second;
            break;
            case "times"
            echo "Answer is: " .$first * $second;
            break; 
            case "divide"
            echo "Answer is: " .$first / $second;
            break;
        }
        

        【讨论】:

          【解决方案5】:

          您还需要将 [== 'add'] 数学运算放在引号中

          if($_POST['group1'] == 'add') {
          echo $first + $second;
          }
          

          完整的代码应该是这样的:

          <?php
          $first = $_POST['first'];
          $second= $_POST['second'];
          if($_POST['group1'] == 'add') {
          echo $first + $second;
          }
          else if($_POST['group1'] == 'subtract') {
          echo $first - $second;
          }
          else if($_POST['group1'] == 'times') {
          echo $first * $second;
          } 
          else if($_POST['group1'] == 'divide') {
          echo $first / $second;
          }
          ?>
          

          【讨论】:

            【解决方案6】:

            使用单引号检查字符串

            例如。 $_POST['group1'] == 'add'

            【讨论】:

              【解决方案7】:
              <?php 
              $result = "";
              class calculator
              {
                  var $a;
                  var $b;
              
                  function checkopration($oprator)
                  {
                      switch($oprator)
                      {
                          case '+':
                          return $this->a + $this->b;
                          break;
              
                          case '-':
                          return $this->a - $this->b;
                          break;
              
                          case '*':
                          return $this->a * $this->b;
                          break;
              
                          case '/':
                          return $this->a / $this->b;
                          break;
              
                          default:
                          return "Sorry No command found";
                      }   
                  }
                  function getresult($a, $b, $c)
                  {
                      $this->a = $a;
                      $this->b = $b;
                      return $this->checkopration($c);
                  }
              }
              
              $cal = new calculator();
              if(isset($_POST['submit']))
              {   
                  $result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
              }
              ?>
              
              <form method="post">
              <table align="center">
                  <tr>
                      <td><strong><?php echo $result; ?><strong></td>
                  </tr>
                  <tr>
                      <td>Enter 1st Number</td>
                      <td><input type="text" name="n1"></td>
                  </tr>
              
                  <tr>
                      <td>Enter 2nd Number</td>
                      <td><input type="text" name="n2"></td>
                  </tr>
              
                  <tr>
                      <td>Select Oprator</td>
                      <td><select name="op">
                          <option value="+">+</option>
                          <option value="-">-</option>
                          <option value="*">*</option>
                          <option value="/">/</option>
                      </select></td>
                  </tr>
              
                  <tr>
                      <td></td>
                      <td><input type="submit" name="submit" value="                =                "></td>
                  </tr>
              
              </table>
              </form>
              

              【讨论】:

                【解决方案8】:
                <!DOCTYPE html>
                    <html lang="en">
                    <head>
                        <meta charset="UTF-8">
                        <title>Calculator</title>
                    </head>
                    <body>
                    HTML Code is here:
                
                         <form method="post">
                            <input type="text" name="numb1">
                            <input type="text" name="numb2">
                            <select name="operator" id="">
                               <option>None</option>
                               <option>Add</option>
                               <option>Subtract</option>
                               <option>Multiply</option>
                               <option>Divide</option>
                               <option>Square</option>
                            </select>
                            <button type="submit" name="submit" value="submit">Calculate</button>
                         </form>
                
                    PHP Code:
                
                        <?php 
                
                            if (isset($_POST['submit'])) {
                                $result1 = $_POST['numb1'];
                                $result2 = $_POST['numb2'];
                                $operator = $_POST['operator'];
                                switch ($operator) {
                                    case 'None':
                                        echo "You need to select any operator";
                                        break;
                                    case 'Add':
                                        echo $result1 + $result2;
                                        break;
                                    case 'Subtract':
                                        echo $result1 - $result2;
                                        break;
                                    case 'Multiply':
                                        echo $result1 * $result2;
                                        break;
                                    case 'Divide':
                                        echo $result1 / $result2;
                                        break;
                                    case 'Square':
                                        echo $result1 ** $result2;
                                        break;
                                }
                            }
                
                
                         ?>
                        enter code here
                
                    </body>
                    </html>
                

                【讨论】:

                • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因
                • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation would greatly improve its long-term value 通过展示为什么这是一个很好的解决问题的方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
                【解决方案9】:

                在 php 文件中进行此更改

                <html>
                <head>
                <meta charset="utf-8">
                <title>Answer</title>
                </head>
                <body>
                <p>The answer is: 
                <?php
                $first = $_POST['first'];
                $second= $_POST['second'];
                if($_POST['group1'] == 'add') {
                echo $first + $second;
                }
                else if($_POST['group1'] == 'subtract') {
                echo $first - $second;
                }
                else if($_POST['group1'] == 'times') {
                echo $first * $second;
                }
                
                else if($_POST['group1'] == 'divide') {
                echo $first / $second;
                }
                else {
                    echo "Enter the numbers properly";
                }
                ?>
                </p> 
                </body>
                </html>
                

                【讨论】:

                • 你好!欢迎来到stackoverflow。您可能想解释一下您的代码,以及您添加了哪些更改。
                • 为 if 语句添加 else 部分。操作 id(add,subtract,...) 必须在引号 ('') 中指定。并且必须计算并打印答案,因此,应删除 echo 后的引号(''),因为 ('') 将打印语句而不是答案。
                【解决方案10】:
                <?php
                $cal1= $_GET['cal1'];
                $cal2= $_GET['cal2'];
                $symbol =$_GET['symbol'];
                
                
                if($symbol == '+')
                {
                    $add = $cal1 + $cal2;
                    echo "Addition is:".$add;
                }
                
                else if($symbol == '-')
                {
                    $subs = $cal1 - $cal2;
                    echo "Substraction is:".$subs;
                }
                
                 else if($symbol == '*')
                {
                    $mul = $cal1 * $cal2;
                    echo "Multiply is:".$mul;
                }
                
                else if($symbol == '/')
                {
                    $div = $cal1 / $cal2;
                    echo "Division is:".$div;
                }
                
                  else
                {
                
                    echo "Oops ,something wrong in your code son";
                }
                
                
                ?>
                

                【讨论】:

                【解决方案11】:
                <!DOCTYPE html>
                <html lang="en" dir="ltr">
                    <head>
                        <meta charset="utf-8">
                        <title>Calculator</title> 
                    </head>
                <body>
                
                    <form method="GET">
                    <h1>Calculator</h1>
                    <p>This is a calculator made by Hau Teen Yee Fabrice and is free to use.</p>
                        <ul>
                     <ol> Multiplication: * </ol>
                     <ol> Substraction: - </ol>
                     <ol> Division: / </ol>
                     <ol> Addition: + </ol>
                        </ul>
                     <p>Enter first number:</p>
                        <input type="text" name="cal1">
                        <p>Enter second number:</p>
                        <input type="text" name="cal2">
                        <p>Enter the calculator symbol</p>
                        <input type="text" name="symbol">
                        <button>Calculate</button>
                    </form>
                    <?php
                    $cal1= $_GET['cal1'];
                    $cal2= $_GET['cal2'];
                    $symbol =$_GET['symbol'];
                
                    if($symbol == '+')
                    {
                        $add = $cal1 + $cal2;
                        echo "Addition is:".$add;
                    }
                    else if($symbol == '-')
                    {
                        $subs = $cal1 - $cal2;
                        echo "Substraction is:".$subs;
                    }
                     else if($symbol == '*')
                    {
                        $mul = $cal1 * $cal2;
                        echo "Multiply is:".$mul;
                    }
                    else if($symbol == '/')
                    {
                        $div = $cal1 / $cal2;
                        echo "Division is:".$div;
                    }
                      else
                    { 
                        echo "Oops ,something wrong in your code son";
                    }
                    ?>
                    </body> 
                </html>
                

                【讨论】:

                  猜你喜欢
                  • 2015-01-28
                  • 1970-01-01
                  • 2015-04-04
                  • 2011-02-13
                  • 2013-06-27
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-04-15
                  相关资源
                  最近更新 更多