-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.php
52 lines (47 loc) · 1.34 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
<?php
if (isset($_POST['num1'], $_POST['num2'], $_POST['operator'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];
switch ($operator) {
case 'add':
$result = $num1 + $num2;
break;
case 'subtract':
$result = $num1 - $num2;
break;
case 'multiply':
$result = $num1 * $num2;
break;
case 'divide':
if ($num2 == 0) {
$result = "Cannot divide by zero!";
} else {
$result = $num1 / $num2;
}
break;
default:
$result = "Invalid operator";
break;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator Result</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h2 class="mt-5">Result</h2>
<?php if (isset($result)) : ?>
<div class="alert alert-success"><?php echo $result; ?></div>
<?php endif; ?>
<a href="index.html" class="btn btn-primary">Back to Calculator</a>
</div>
</body>
</html>