PHP计算器

<html>
  <head>
 <!--  <meta charset="utf-8"> -->
  </head>
  <body>
    <?php
       $mess="";
       if(isset($_POST["sub"])){
           if($_POST["num1"]==""){
               $mess.="第一个数不能为空!<br>";
           }else{
               if(!is_numeric($_POST["num1"])){
                   $mess.="第一个数必须为数字!<br>";
               }
           }
           if($_POST["num2"]==""){
               $mess.="第二个数不能为空!<br>";
           }else{
               if(!is_numeric($_POST["num2"])){
                   $mess.="第二个数必须为数字!<br>";
               }else{
                   if($_POST["opt"]=="/"&&$_POST["num2"]==0){
                       $mess.="除数不能为0";
                   }
               }
           }
       }
    ?>
   
<table border="1" align="center" width="400">
<form action="index.php" method="post">
  <caption>計算器</caption>
  <tr>
          <td>
            <input type="text" size="4" name="num1" value="<?php if(isset($_POST["num1"])){echo $_POST["num1"];}?>" />
          </td>
          <td>
            <select name="opt">
              <option  <?php if(isset($_POST["opt"])&& $_POST["opt"]=="+")echo"selected='selected'"; ?>>+</option>
              <option  <?php if(isset($_POST["opt"])&& $_POST["opt"]=="-")echo"selected='selected'"; ?>>-</option>
              <option <?php if(isset($_POST["opt"])&& $_POST["opt"]=="+")echo"selected='selected'"; ?>>*</option>
              <option <?php echo isset($_POST["opt"])&&$_POST["opt"]=="/"?"selected":"" ?>>/</option>
              <option <?php echo isset($_POST["opt"])&&$_POST["opt"]=="%ss"?"selected":"" ?>>%</option>
            </select>
          </td>
          <td>
           <input type="text" size="4" name="num2"value="<?php if(isset($_POST["num2"])){echo $_POST["num2"];}?>" />
          </td>
          <td>
          <input type="submit" name="sub" value="计算"/>
          </td>
  </tr>
</form>
          <?php
      if(isset($_POST["sub"])){
          echo "<tr><td colspan='4'>";
          if(!$mess){
            $sum=0;
            switch($_POST["opt"]){
                case "+":
                    $sum=$_POST["num1"]+$_POST["num2"];
                    break;
                case "-":
                    $sum=$_POST["num1"]-$_POST["num2"];
                    break;
                case "*":
                    $sum=$_POST["num1"]*$_POST["num2"];
                    break;
                case "/":
                    $sum=$_POST["num1"]/$_POST["num2"];
                    break;
                case "%":
                    $sum=$_POST["num1"]%$_POST["num2"];
                    break;
            }echo"结果:{$_POST['num1']}{$_POST['opt']}{$_POST['num2']}={$sum}";
          }else{
                echo"$mess";
            }
       
         echo"</td></tr>";
      }
      ?>    
</table>

    
  </body>
</html>
写这个计算器select name 属性老提示未定义,用isset()函数检测选择
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值