设计一个计算的功能,该功能能够完成运算并且能够对不合理的数据进行验证并且给出错误提示.

规则: 第一个数,第二个数不能够为空

如果操作符是/,第二个数数不能够为0.

 <?php
header('Content-Type:text/html; charset=utf-8');
/*设计一个计算的功能,该功能能够完成运算并且能够对不合理的数据进行验证并且给出错误提示.
规则: 第一个数,第二个数不能够为空
如果操作符是/,第二个数数不能够为0.*/ function jsq($num1,$oper,$num2){
//检测数据不能为空并提示
if(!isset($num1) || !is_numeric($num1)){
$error = <<<ERROR
<script>
alert('第一个数不合法');
</script>
ERROR;
return $error;
}
if(!isset($num2) || !is_numeric($num2)){
$error = <<<ERROR
<script>
alert('第二个数不合法');
</script>
ERROR;
return $error;
} if($oper == "+"){
$result = $num1 + $num2;
}elseif($oper == "-"){
$result = $num1 - $num2;
}elseif($oper == "*"){
$result = $num1 * $num2;
}elseif($oper = "/"){
if($num2 == 0){
$error = <<<ERROR
<script>
alert('第二个数不能为0');
</script>
ERROR;
return $error;
}
$result = $num1 / $num2;
}
return $result;
} if($_SERVER['REQUEST_METHOD'] == "POST"){
$res = jsq($_POST['num1'],$_POST['oper'],$_POST['num2']);
}
?> <h2>用php写的基础计算器</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
第一个数:<input type="text" name="num1" /><p>
操作符:<select name="oper">
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> * </option>
<option value="/"> / </option>
</select><p>
第二个数:<input type="text" name="num2" /><p>
<input type="submit" value="计算" /> </form>
计算结果为:<?php echo isset($res)?$res:""; ?>

PHP的基础计算器的更多相关文章

  1. LeetCode OJ:Basic Calculator(基础计算器)

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  2. vuejs基础-计算器案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 神奇的计算器dc和bc

    神奇的计算器dc和bc Linux就这个范儿 P244http://baike.baidu.com/link?url=YlTtivBfc1tfzreeUQoe8D0C6yqwbZGqnmlObq1hj ...

  4. Linux之初识磁盘

    磁盘知识体系概括 机械硬盘和固态硬盘 机械磁盘剖开图 磁盘工作的视频动画,主轴转动,机械手读写 模拟磁盘工作视频,点击中间三角播放 磁盘结构详解 磁盘外部结构 组成 主要由三部分组成:盘片.主轴(机械 ...

  5. Linux —— 常见指令及其英文全称

    alias:给命令起别名 awk = "Aho Weiberger and Kernighan" ,三个作者的姓的第一个字母 bash:GNU Bourne-Again Shell ...

  6. Linux常用命令及其英文全称

      alias:给命令起别名 awk = "Aho Weiberger and Kernighan" ,三个作者的姓的第一个字母 bash:GNU Bourne-Again She ...

  7. Linux必备工具与软件包

    yum -y update(所有都升级和改变) 升级所有包,系统版本和内核,改变软件设置和系统设置 ----------------------------------------------- yu ...

  8. 5.3 Spring5源码--Spring AOP使用接口方式实现

    Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...

  9. 5.2 Spring5源码--Spring AOP源码分析二

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

随机推荐

  1. oschina git 私有项目使用。。

    工作流程 安装设置 git 下载最新版本的git http://git-scm.com/downloads 当你安装完成git的时候,你需要简单的配置一下,打开终端: 用户名 第一步,你需要告诉git ...

  2. [Effective C++ --033]避免遮掩继承而来的名称

    这一章一直在想怎么写,因为比较基础,很容易理解,就按照需要来写吧. 假设我们有这样一个类: class Base { private: int x; public: ; virtual void mf ...

  3. QFtp类参考

    QFtp是一个用来实现FTP协议的类. 详情请见…… #include <qftp.h> 继承了QNetworkProtocol. 所有成员函数的列表. 公有成员 QFtp () virt ...

  4. 10+ commonly using find command switches with example Unix/Linux

    http://crybit.com/find-command-usage-with-example-unixlinux/ find command is one of the best search ...

  5. Javascript 数组与字典

    Javascript 的数组Array,既是一个数组,也是一个字典(Dictionary). 先举例看看数组的用法. var a = new Array(); a[0] = "Acer&qu ...

  6. css笔记11:选择器练习

    1. (1)exam1.css文件: .s1 { font-size: 50px; color: blue; } .s2 { backgoround:gray; font-style: italic; ...

  7. android开发之路06(浅谈单例设计模式)

    设计模式之单例模式: 一.单例模式实现特点:①单例类在整个应用程序中只能有一个实例(通过私有无参构造器实现):②单例类必须自己创建这个实例并且可供其他对象访问(通过静态公开的访问权限修饰的getIns ...

  8. python(4) - 装饰器2

    接下来修改一下上一篇的login,将用户名传递给验证函数. def login(func): #接收一个函数作为参数 def inner(name): print("用户验证通过....&q ...

  9. Angular学习资源汇集

    网站 官网:www.angularjs.org 中文官网(社区):www.angularjs.cn 博客 流浪猫的窝 粉丝日记:AngularJS体验式编程系列文章 一介布衣 文档 GitCafe A ...

  10. Halcon C++混合编程学习之Qt 实现检测焊接点

    1 # include "HalconCpp.h" # include "HDevThread.h" # include <X11/Xlib.h> ...