PHP实现简单的计算器

这篇文章主要为大家详细介绍了PHP实现简单的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了PHP实现简单计算器的具体代码,供大家参考,具体内容如下

caculator.php


<html>
<head>
 
</head>
<body>
 
<?php
if (!empty($_POST)) {
  $data1 = $_POST["data1"];
  $data2 = $_POST["data2"];
  $fuhao = $_POST["fuhao"];
  $result = "";
 
  echo $data1;
  echo $data2;
  echo $fuhao;
  echo $result;
 
 
  if ($fuhao == '+') {
    $result = $data1 + $data2;
  } else if ($fuhao == '-') {
    $result = $data1 - $data2;
  } else if ($fuhao == '*') {
    $result = $data1 * $data2;
  } else if ($fuhao == '/') {
    $result = $data1 / $data2;
  }
} else {
  $data1 = "";
  $data2 = "";
  $fuhao = "";
  $result = "";
}
 
?>
 
<form action="caculator.php" method="post">
  <input type="text" name="data1" value="<?php echo $data1; ?>"/>
  <select name="fuhao">
 
    <option value="+"
        <?php if ($fuhao == "+") {
          echo 'selected="selected"';
        } ?>>+
    </option>
    <option value="-"
        <?php if ($fuhao == "-") {
          echo 'selected="selected"';
        } ?>>-
    </option>
    <option value="*"
        <?php if ($fuhao == "*") {
          echo 'selected="selected"';
        } ?>>*
    </option>
    <option value="/"
    <?php if ($fuhao == "/") {
      echo 'selected="selected"';
    } ?>>/
    </option>
 
    </option>
  </select>
  <input type="text" name="data2" value="<?php echo $data2; ?>"/>
  <input type="submit" value="="/>
  <input name="result" type="text" value="<?php echo $result; ?>"/>
  <input type="reset"/>
 
</form>
 
 
</body>
</html>

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。

本文标题为:PHP实现简单的计算器

基础教程推荐