Implement a simple calculator in php

不言
Release: 2023-03-23 06:46:02
Original
7979 people have browsed it

This article introduces the code to implement a simple calculator using PHP. I share it with you here and give it to friends in need. Let’s take a look together.

<html>
<head>
    <title>PHP实现简单的计算器</title>
    <meata http-equiv="Content-Type" content="text/html;charset=utf-8"></meata>
</head>
<?php
    $num1=true;
    $num2=true;
    $numa=true;
    $numb=true;
    $message="";
    //单路分支
    if(isset($_GET["sub"])){
        if($_GET["num1"]==""){
            $num1=false;
            $message.="第一个数字不能为空";
        }
        if(!is_numeric($_GET["num1"])){
            $numa=false;
            $message.="第一个不是数字";
        }
        if($_GET["num2"]==""){
            $num2=false;
            $message.="第二个不能为空";
        }
        if(!is_numeric($_GET["num2"])){
            $numb=false;
            $message.="第二个不是数字";
        }
        if($num1 && $num2 && $numa && $numb){
            $sum=0;
            //多路分支swith
            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" sidth="500">
    <caption><h1>计算器</h1></caption>
    <form action="test.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>
        </tr>

        <?php
            if(isset($_GET["sub"])){
                echo &#39;<tr><TD colspan="5">&#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>


/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/3/27
 * Time: 16:26
 *
 */
Copy after login

Related recommendations:

How to implement the calculator function in javascript

How to implement a simple calculator in HTML


The above is the detailed content of Implement a simple calculator in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!