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

php 简单计算器

May 25, 2016 pm 04:58 PM

  1.   作一个加法器,计算它们的和
    2.  作一个减法器,计算它们的差
    3.  作一个乘法器,计算它们的积
    4.  作一个除法器,计算它们的商(和余数)
    这个计算器有着俩个页面,一个页面用于输出结果

mycal.php

<html>
<head>
    <title>我的计算器</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
 
</head>
<body>
    <form action="result.php" method="post"/>
    <table width="300px" border="0">  
    <tr><td>第一个数</td><td><input type="text" name="num1" /></td></tr>
    <tr><td>第二个数</td><td><input type="text" name="num2" /></td></tr>
    <tr><td>运算符</td><td>
    <select name="oper">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
    </select></td></tr>
    <tr>
        <td colspan="2"><input type="submit" value="计算结果"></td>
    </tr>
    </table>
    </form>
</body>
</html>
Copy after login

result.php

<?php
//总结一句话:
//我们按照正常的思路,应当保证接收数据的时候,$_REQUEST
//[&#39;参数&#39;]要和提交数据的页面给出html元素名字一致,如果不一致,
//则会出现notice提示,同时我们接收的数据就是null等价""
/*
    if($num1==null){
        echo&#39;也没有收到值&#39;;
    }
    if($num1==""){
        echo&#39;也没有值&#39;;
    }
    echo $num1;
*/
    //接收用户从mycal.php(对应静态页面 浏览器提交的数据
    //1.接收num1
    //$_REQUEST 该方法可以接收用户的post 或者get 请求数据
    $num1=$_REQUEST[&#39;num1&#39;];
    //2.接收num2
    $num2=$_REQUEST[&#39;num2&#39;];
    //3.接收运算符
    $oper=$_REQUEST[&#39;oper&#39;];
 
    $res=0;
    switch($oper){
        case "+":
            $res=$num1+$num2;
            break;
        case "-":
            $res=$num1-$num2;
            break;
        case "*":
            $res=$num1*$num2;
            break;
        case "/":
            $res=$num1/$num2;
            break;
        default:
            echo &#39;运算结果不正常&#39;;
 
    }
    echo&#39;接收到.&#39;.$num1."||".$num2."||".$oper."<br/>";
    echo &#39;结果=&#39;.$res ;
?> 
<br/>
<a href="mycal.php">返回计算器页面</a>
Copy after login
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)