php 简单计算器
简单计算器 刚开始学PHP,这个做练习代码。**calculator.php**[代码片段(70行)]
刚开始学PHP,这个做练习代码。
calculator.php
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf8">
<title>简单计算器</title>
</head>
<?php
$num1 = 0;
$num2 = 0;
$res = 0;
$message;
$flag=false;
if(isset($_GET["sub"])){
if(!$_GET["num1"]=="" && !$_GET["num2"]==""){
if((is_numeric($_GET["num1"])&&is_numeric($_GET["num2"]))){
$flag = true;
$num1 = $_GET["num1"];
$num2 = $_GET["num2"];
switch($_GET["type"]){
case "+":
$res = $num1 + $num2;
break;
case "-":
$res = $num1 - $num2;
break;
case "*":
$res = $num1 * $num2;
break;
case "/":
$res = $num1 / $num2;
break;
case "%":
$res = $num1 % $num2;
break;
}
}else $message = "注意!运算数必须为数字!";
}else $message = "注意!不能留空!";
}
?>
<body>
<br /><br /><br />
<center><h1>计算器</h1></center><br /><br />
<form action="calculator.php">
<table align="center">
<tr>
<td><input type="text" size="5" name="num1" value="<?php echo $_GET["num1"]?>"></td>
<td>
<select name="type">
<option value="+" <?php echo $_GET["type"]=="+"?"selected":""?>>+</option>
<option value="-" <?php echo $_GET["type"]=="-"?"selected":""?>>-</option>
<option value="*" <?php echo $_GET["type"]=="*"?"selected":""?>>*</option>
<option value="/" <?php echo $_GET["type"]=="/"?"selected":""?>>/</option>
<option value="%" <?php echo $_GET["type"]=="%"?"selected":""?>>%</option>
</select>
</td>
<td><input type="text" size="5" name="num2" value="<?php echo $_GET["num2"]?>"></td>
<td>=</td>
<td><input type="text" size="8" name="res" value="<?php echo $flag?$res:"" ?>"></td>
<td><input type="submit" size="7" name="sub" value="计算"></td>
</tr>
<tr>
<td><br /><br /><br /><br /></td>
<td colspan="6" align="left"> <font color="red"><?php echo !$flag?$message:""?></font></td>
</tr>
</table>
</form>
</body>
</html>
精彩图集
精彩文章






