-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.php
More file actions
66 lines (57 loc) · 2.04 KB
/
calculator.php
File metadata and controls
66 lines (57 loc) · 2.04 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width= , initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" >
<input type="number" name="num01" placeholder="Number one" required>
<select name="operator">
<option value="add">+</option>
<option value="subtract">+</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<input type="number" name="num02" placeholder="Number two" required>
<button>Calculate</button>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$num01 = filter_input(INPUT_POST,"num01",FILTER_SANITIZE_NUMBER_FLOAT);
$num02 = filter_input(INPUT_POST,"num02",FILTER_SANITIZE_NUMBER_FLOAT);
$operator = htmlspecialchars($_POST["operator"]);
$errors = false;
if(empty($num01) || empty($num02) || empty($operator)){
echo "Fill in all Fields";
$errors = true;
}
if(!is_numeric($num01) || !is_numeric($num02)){
echo "Plz enter number";
$errors =true;
}
if(!$errors){
$value = 0;
switch($operator){
case "add":
$value = $num01 + $num02;
break;
case "subtract":
$value = $num01 - $num02;
break;
case "multiply":
$value = $num01 * $num02;
break;
case "divide":
$value = $num01 / $num02;
break;
default :
echo "Something went wrong!";
};
echo "<p>Result = $value </p>";
}
}
?>
</body>
</html>