Home > php教程 > PHP源码 > 简单计算器

简单计算器

PHP中文网
Release: 2016-05-23 08:39:36
Original
1234 people have browsed it

php代码

<html>
        <head>
            <title>
                PHP实现简单计算器
            </title>
            <meta http-equiv="content-type" content="text/html;charset=utf-8">
        </head>
        <?php
            if(isset($_GET["sub"])){
                $sum=0;
                $num1=true;
                $num2=true;
                $numa=true;
                $numb=true;
                $message="";
                if($_GET["num1"]==""){
                    $num1=false;
                    $message="第一个参数为空";
                }
                if($_GET["num2"]==""){
                    $num2=false;
                    $message="第二个参数为空";
                }
                if(!is_numeric($_GET["num1"])){
                    $numa=false;
                    $message.="第一个参数不是数字!";
                }
                if(!is_numeric($_GET["num2"])){
                    $numb=false;
                    $message.="第二个参数不是数字!";
                }
                if($num1 && $num2 && $numa){
                    switch($_GET["ysf"]){
                        case "+":$sum=$_GET["num1"]+$_GET["num2"];
                            break;
                        case "-":$sum=$_GET["num1"]-$_GET["num2"];
                            break;
                        case "*":$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 echo $_GET["ysf"]=="+"? "selected":""?>>+</option>
                                <option value="-" <?php echo $_GET["ysf"]=="-"? "selected":""?>>-</option>
                                <option value="*" <?php echo $_GET["ysf"]=="*"? "selected":""?>>*</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>
Copy after login
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template