forked from WatanabeHiroki0903/watanabe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdentaku.php
76 lines (60 loc) · 1.4 KB
/
dentaku.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dentaku</title>
</head>
<body>
<form action ="dentaku.php" method="post">
<input type="number" name="first">
<select name="kigou">
<option value="+">+</option>
<option value="-">-</option>
<option value="×">×</option>
<option value="÷">÷</option>
</select>
<input type="number" name="second">
<input type="submit" value="計算する">
</form>
<?php
$errors=[];
if(isset($_POST["first"]) && isset($_POST["kigou"]) && isset($_POST["second"])){
if(!is_numeric($_POST["first"]) || !is_numeric($_POST["second"])){
$errors[]="数値を二つ入力してください。";
};
$kigouList=["+", "-", "×", "÷"];
if(!in_array($_POST["kigou"], $kigouList)){
$errors[]="記号の値が正しくありません。";
};
if(($_POST["kigou"]==="÷") && ($_POST["second"]==="0")){
$errors[]="0では割れません。";
}
if(count($errors)>0){
echo "<hr>";
foreach($errors as $value){
echo $value, "<br>";
exit();
};
};
$first=$_POST["first"];
$second=$_POST["second"];
$kigou=$_POST["kigou"];
switch($kigou){
case "+":
$result=$first + $second;
break;
case "-":
$result=$first - $second;
break;
case "×":
$result=$first * $second;
break;
case "÷":
$result=$first / $second;
break;
};
echo "<hr>", $first, $kigou, $second, "=", $result;
};
?>
</body>
</html>