PHP容易计算器

WBOY
Release: 2016-06-13 11:33:08
Original
1275 people have browsed it

PHP简单计算器

这个是运用PHP的条件控制语句写的,比较简单,消化下...

			<title>PHP实现简单计算器(使用分支结构)</title>				<?php $mess = "";			if( isset( $_POST["sub"])){				if( $_POST["num1"] == ""){					$mess .= "第一个数不能为空!<br>";				}				else if( !is_numeric($_POST["num1"])){					$mess .= "第一个数必须为数字!<br>";				}							if( $_POST["num2"] == ""){					$mess .= "第二个数不能为空!<br>";				}				else{					if( !is_numeric($_POST["num2"])){						$mess .= "第二个数必须为数字!<br>";					}					else if( $_POST["opt"] == "/" && $_POST["num2"] == 0){						$mess .= "除数不能为0";					}				}			}		?>	
Copy after login
'; } ?>

计算器

" /> " />
'; if( !$mess){ $sum = 0; switch($_POST["opt"]){ case "+": $sum = $_POST["num1"] + $_POST["num2"];break; case "-": $sum = $_POST["num1"] - $_POST["num2"];break; case "x": $sum = $_POST["num1"] * $_POST["num2"];break; case "/": $sum = $_POST["num1"] / $_POST["num2"];break; case "%": $sum = $_POST["num1"] % $_POST["num2"];break; } echo "结果: {$_POST['num1']} {$_POST['opt']} {$_POST['num2']} = {$sum}"; } else{ echo $mess; } echo '



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