Home  >  Article  >  Backend Development  >  How to implement calculator code in php

How to implement calculator code in php

藏色散人
藏色散人Original
2020-08-17 09:40:433321browse

php method to implement a calculator: first create a PHP sample file; then use the if statement to determine whether the post has passed the sub parameter; then use the switch statement to determine whether the two operands are numbers; finally create a simple calculation The HTML code of the server is sufficient.

How to implement calculator code in php

Recommended: "PHP Video Tutorial"

<?php  
    // 判断post是否传过来sub参数,从而判断是提交计算,还是刷新页面  
    if (isset($_POST[&#39;sub&#39;])){  
        echo "用户点击提交按钮,提交计算请求<br>";  
  
// 下面注释掉的代码并不是错误的,只是区别switch的另外一种方式  
//        if ($_POST[&#39;ysf&#39;] == "+"){  
//            echo $_POST[&#39;num1&#39;] + $_POST[&#39;num2&#39;] . "<br>";  
//        }elseif ($_POST[&#39;ysf&#39;] == "-"){  
//            echo $_POST[&#39;num1&#39;] - $_POST[&#39;num2&#39;] . "<br>";  
//        }elseif ($_POST[&#39;ysf&#39;] == "*"){  
//            echo $_POST[&#39;num1&#39;] * $_POST[&#39;num2&#39;] . "<br>";  
//        }elseif ($_POST[&#39;ysf&#39;] == "/"){  
//            echo $_POST[&#39;num1&#39;] / $_POST[&#39;num2&#39;] . "<br>";  
//        }elseif ($_POST[&#39;ysf&#39;] == "%"){  
//            echo $_POST[&#39;num1&#39;] % $_POST[&#39;num2&#39;] . "<br>";  
//        }  
  
        // 判断两个运算元是否为数字 如果不是数字 则不进行运算  
        if (!is_numeric($_POST[&#39;num1&#39;]) || !is_numeric($_POST[&#39;num2&#39;])){  
            $isDo = false;  
            echo "其中一个运算元不是数字,不进行运算<br>";  
        }else{  
            $isDo = true;  
        }  
  
        // 声明变量 计算结果  
        $sum = "";  
  
        if ($isDo){ // 判断两个运算元是否为数字 如果不是数字 则不进行运算  
            switch ($_POST[&#39;ysf&#39;]){  
                case &#39;+&#39;:  
                    $sum = $_POST[&#39;num1&#39;] + $_POST[&#39;num2&#39;];  
                    break;  
                case &#39;-&#39;:  
                    $sum = $_POST[&#39;num1&#39;] - $_POST[&#39;num2&#39;];  
                    break;  
                case &#39;*&#39;:  
                    $sum = $_POST[&#39;num1&#39;] * $_POST[&#39;num2&#39;];  
                    break;  
                case &#39;/&#39;:  
                    $sum = $_POST[&#39;num1&#39;] / $_POST[&#39;num2&#39;];  
                    break;  
                case &#39;%&#39;:  
                    $sum = $_POST[&#39;num1&#39;] % $_POST[&#39;num2&#39;];  
                    break;  
            }  
            echo $sum."<br>";  
        }  
    }else{  
        echo "用户刷新页面<br>";  
        //die("不做计算处理<br>");  
    }  
?>  
  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>简易计算器</title>  
</head>  
<body>  
<!--table 表格-->  
<!--border=1,表格周围的边框设置为1像素宽-->  
<!--width=400,表格宽度为400像素宽-->  
<!--align=center,表格水平对齐方式为居中对齐内容-->  
<!--caption 定义表格标题-->  
<!--<h1> 定义最大的标题。<h6> 定义最小的标题。-->  
<!--<tr> 标签定义 HTML 表格中的行。包含一个或多个th或td元素。-->  
<!--ysf 运算符的首字母。-->  
<!--colspan 规定单元格可横跨的列数。-->  
<table border="1" width="400" align="center">  
    <form action="jsq2.php" method="post">  
        <caption><h1>简易计算器</h1></caption>  
        <caption><h4>iwanghang</h4></caption>  
        <tr>  
            <!--第一个运算元-->  
<!--            <td><input type="text" size="5" name="num1" value=""></td>-->  
            <td><input type="text" size="5" name="num1" value="<?php  
                if (isset($_POST[&#39;sub&#39;])){echo $_POST[&#39;num1&#39;];} ?>"></td>  
            <!--运算符-->  
            <td>  
                <select name="ysf">  
<!--                    <option value="+"> + </option>-->  
<!--                    <option value="-"> - </option>-->  
<!--                    <option value="*"> * </option>-->  
<!--                    <option value="/"> / </option>-->  
<!--                    <option value="%"> % </option>-->  
                    <option <?php if (isset($_POST[&#39;sub&#39;])){  
                        if ($_POST[&#39;ysf&#39;]=="+") echo "selected";} ?> value="+"> + </option>  
                    <option <?php if (isset($_POST[&#39;sub&#39;])){  
                        if ($_POST[&#39;ysf&#39;]=="-") echo "selected";} ?> value="-"> - </option>  
                    <option <?php if (isset($_POST[&#39;sub&#39;])){  
                        if ($_POST[&#39;ysf&#39;]=="*") echo "selected";} ?> value="*"> * </option>  
                    <option <?php if (isset($_POST[&#39;sub&#39;])){  
                        if ($_POST[&#39;ysf&#39;]=="/") echo "selected";} ?> value="/"> / </option>  
                    <option <?php if (isset($_POST[&#39;sub&#39;])){  
                        if ($_POST[&#39;ysf&#39;]=="%") echo "selected";} ?> value="%"> % </option>  
                </select>  
            </td>  
            <!--第二个运算元-->  
<!--            <td><input type="text" size="5" name="num2" value=""></td>-->  
            <td><input type="text" size="5" name="num2" value="<?php  
                if (isset($_POST[&#39;sub&#39;])){echo $_POST[&#39;num2&#39;];} ?>"></td>  
            <!--提交-->  
            <td><input type="submit" name="sub" value="等于"></td>  
        </tr>  
        <tr>  
            <td colspan="4">  
                <?php  
                    if (isset($_POST[&#39;sub&#39;])){  
                        echo "计算结果:{$_POST[&#39;num1&#39;]}{$_POST[&#39;ysf&#39;]}{$_POST[&#39;num2&#39;]} = {$sum}";  
                    }  
                ?>  
            </td>  
        </tr>  
    </form>  
</table>  
</body>  
</html>

The above is the detailed content of How to implement calculator code 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