search
Homephp教程PHP源码简单计算器

简单计算器

May 23, 2016 am 08:39 AM
calculator

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>
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor