Home  >  Article  >  Backend Development  >  How to implement simple web calculator function in php

How to implement simple web calculator function in php

墨辰丷
墨辰丷Original
2018-05-23 14:51:154823browse

This article mainly introduces PHP programming to implement simple web-based calculator functions, involving PHP simple form operations and numerical calculations related implementation techniques. Friends in need can refer to

How to implement it through PHP code The simple functionality of a web-based calculator? The following is a web version of the calculator built with the basic knowledge of PHP. The only function is "addition, subtraction, remainder, division and remainder". Some other calculation functions can be expanded.

The specific php code to implement a simple calculator is as follows (this is the entire code of the php file jsq.php):

<html>
   <head>
   <title>这是一个网页版的简单计算器</title>
     <meta http-equiv="Content-Type" content="text/html;chaset=utf-8" >
   </head>
<?php
   $num1=true;
   $num2=true;
   $numa=true;
   $numb=true;
   $message="";
   if(isset($_GET["sub"])){
     if($_GET["num1"]==""){
       $num1=false;
       $message.="第一个数不能为空";
     }
     if($_GET["num2"]==""){
       $num1=false;
       $message.="第二个数不能为空";
     }
     if(!is_numeric($_GET["num1"])){
    $numa=false;
      $message.="第一个数应该是数字";
     }
     if(!is_numeric($_GET["num2"])){
    $numb=false;
      $message.="第二个数应该是数字";
     }
     if($num1 && $num2 && $numa && $numb){
       $sum=0;
       switch($_GET["ysf"]){
          case "+":
         $sum=$_GET["num1"]+$_GET["num2"];
         break;
          case "-":
         $sum=$_GET["num1"]-$_GET["num2"];
         break;
          case "x":
         $sum=$_GET["num1"]*$_GET["num2"];
         break;
          case "/":
         $sum=$_GET["num1"]/$_GET["num2"];
         break;
          case "%":
         $sum=$_GET["num1"]%$_GET["num2"];
         break;
       }
       }
   }
?>
   <body>
   <table align="center" border="1" width="500">
        <caption><h1>网页计算器</h1></caption>
        <form action="jsq.php">
        <tr>
           <td>
       <input type="text" size="5" name="num1" value="<?php echo $_GET["num1"] ?>">
           </td>
           <td>
              <select name="ysf">
        <option value="+" <?php if($_GET["ysf"]=="+") echo "selected" ?>>+</option>
                  <option value="-" <?php if($_GET["ysf"]=="-") echo "selected" ?>>-</option>
          <option value="x" <?php echo $_GET["ysf"]=="x"?"selected":"" ?>>x</option>
          <option value="/" <?php echo $_GET["ysf"]=="/"?"selected":"" ?>>/</option>
          <option value="%" <?php echo $_GET["ysf"]=="%"?"selected":"" ?>>%</option>
        </select>
           </td>
           <td>
        <input type="text" size="5" name="num2" value="<?php echo $_GET["num2"] ?>">
       </td>
           <td>
              <input type="submit" name="sub" value="计算">
       </td>
        <?php
        if(isset($_GET["sub"])){
      echo &#39;<tr><td colspan="4">&#39;;
      if($num1 && $num2 && $numa && $numb){
               echo "结果:".$_GET["num1"]." ".$_GET["ysf"]." ".$_GET["num2"]." = ".$sum;
      }else{
         echo $message;
      }
         echo &#39;</td></tr>&#39;;
       }
        ?>
        </form>
     </table>
   </body>
</html>

Note: The above file jsq.php needs to be uploaded to the root directory of the web server in order to be correctly interpreted and executed.

The effect is shown in the picture:

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPSimple way to generate txt files to a specified directory_php tips

PHP Detailed explanation of clue binary tree and binary tree traversal method implemented_php skills

PHP Detailed explanation of methods of crawling and analyzing web pages_php skills

The above is the detailed content of How to implement simple web calculator function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn