-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.php
59 lines (59 loc) · 1.62 KB
/
calculator.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
<?php
mb_language('ja');
mb_internal_encoding('UTF-8');
?>
<!DOCTYPE html>
<html>
<head>
<title>電卓</title>
</head>
<body>
<form action="" method="post">
<h2 align="center">Please input calculation</h2>
<p align="center">
<input type="text" name="first" size="5">
<select name="str" size="1">
<option value=""></option>
<option value="+">+</option>
<option value="-">-</option>
<option value="×">×</option>
<option value="÷">÷</option>
<input type="text" name="second" size="5">
<br>
<input type="submit" value="計算"><input type="reset" value="リセット">
</p>
</form>
<hr>
<p align="center">
<?php
if(!isset($_POST['first']) || !isset($_POST['second'])) {//check undifined
echo "please input number";
}elseif(empty($_POST['str'])){
echo "please select operator symbol";
}else{
if(ctype_digit($_POST['first']) && ctype_digit($_POST['second'])) {
$var1 = (int)$_POST['first'];
$var2 = $_POST['str'];
$var3 = (int)$_POST['second'];
if ($var2 == '+') {
$result = $var1 + $var3;
} elseif ($var2 == '-') {
$result = $var1 - $var3;
} elseif ($var2 == '×') {
$result = $var1 * $var3;
} elseif ($var2 == '÷' && $var3 != 0) {
$result = $var1 / $var3;
}
if($var2 == '÷' && $var3 == 0){
print_r("Can't devided by 0!!");
}else{
echo $var1 . $var2 . $var3 . "=" . $result;
}
}else{
echo "You can input only number!!";
}
}
?>
</p>
</body>
</html>