本篇文章介绍的是用php实现简单计算器的代码,在这里分享给大家,也给有需要的朋友参考一下,大家一起来看一看吧
<html>
<head>
<title>php实现简单的计算器</title>
<meata http-equiv="content-type" content="text/html;charset=utf-8"></meata>
</head>
<?php
$num1=true;
$num2=true;
$numa=true;
$numb=true;
$message="";
//单路分支
if(isset($_get["sub"])){
if($_get["num1"]==""){
$num1=false;
$message.="第一个数字不能为空";
}
if(!is_numeric($_get["num1"])){
$numa=false;
$message.="第一个不是数字";
}
if($_get["num2"]==""){
$num2=false;
$message.="第二个不能为空";
}
if(!is_numeric($_get["num2"])){
$numb=false;
$message.="第二个不是数字";
}
if($num1 && $num2 && $numa && $numb){
$sum=0;
//多路分支swith
switch($_get["ysf"]){
case "+":
$sum=$_get["num1"]+$_get["num2"];
break;
case "-":
$sum=$_get["num1"]-$_get["num2-"];
break;
case "x":
$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" sidth="500">
<caption><h1>计算器</h1></caption>
<form action="test.php">
<tr>
<td>
<input type="text" size="5" name="num1" value="<?php echo $_get[num1] ?>"
</td>
<td>
<select name="ysf">
<option value="+" <?php if($_get["ysf"]=="+") echo "selected" ?>>+</option>
<option value="-" <?php if($_get["ysf"]=="-") echo "selected" ?>>-</option>
<option value="x" <?php echo $_get["ysf"]=="x"?"selected":"" ?>>x</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 '<tr><td colspan="5">';
if($num1 && $num2 && $numa && $numb){
echo "结果:".$_get["num1"]." ".$_get["ysf"]." ".$_get["num2"]." = ".$sum;
}else{
echo $message;
}
echo '</td></tr>';
}
?>
</form>
</table>
</body>
</html>
/**
* created by phpstorm.
* user: user
* date: 2018/3/27
* time: 16:26
*
*/
相关推荐:
javascript如何实现计算器功能
html怎样实现简单计算器
以上就是php实现简单的计算器的详细内容。