作业内容:
1、练习下这堂课学的
2、计算机的if 改为 switch
3、自己增加下判断,不让结果为0, 或者 判断 传值的时候 不要小于0
<?php
$num1 = "";
$num2 = "";
$opt = "";
$result = 0;
foreach($_GET as $k => $v){
$$k = $v;
}
if($num2 == 0 and ($opt == 4 or $opt == 5)){
echo "除数不能为0";
}else{
$num1 = (int)$num1;
$num2 = (int)$num2;
switch ($opt) {
case '1':
$result = $num1 + $num2;
break;
case '2':
$result = $num1 - $num2;
break;
case '3':
$result = $num1 * $num2;
break;
case '4':
$result = $num1 / $num2;
break;
case '5':
$result = $num1 % $num2;
break;
default:
break;
}
}
?>
<!DOCTYPE html>
<html lang="zh_CH">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP计算器</title>
<style>
*{
margin: 5px;
padding: 5px;
font-size: 20px;
height: 36px;
}
.input{
width: 100px;
}
.input2{
width: 100px;
}
select{
width: 50px;
}
</style>
</head>
<body>
<form action="" method="GET">
<input class="input" type="number" name="num1" value="<?=$num1; ?>">
<select name="opt">
<?php
$opts = ['+', '-', '*', '/', '%'];
$html = "";
foreach($opts as $k => $v){
if(isset($_GET['opt']) && $_GET['opt'] == $k+1){
$html .= '<option value="'.($k+1).'" selected>'.$v.'</option>';
}else{
$html .= '<option value="'.($k+1).'">'.$v.'</option>';
}
}
echo($html);
?>
</select>
<input class="input" type="number" name="num2" value="<?=$num2;?>">
<label class="input"><?=($result != "")?"= ".$result:""?></label>
<!-- <br> -->
<input class="input2" type="submit" value="计算">
<input class="input2" type="button" value="重置" onclick="location.href='<?=$_SERVER['PHP_SELF'];?>'">
</form>
</body>
</html>