<?php
function add($a, $b) {
return $a + $b;
}
function subtract($a, $b) {
return $a - $b;
}
function multiply($a, $b) {
return $a * $b;
}
function divide($a, $b) {
if ($b == 0) {
return "Cannot divide by zero";
}
return $a / $b;
}
if (isset($_POST['calculate'])) {
$value1 =
$_POST['value1'];
$value2 =
$_POST['value2'];
$operation =
$_POST['operation'];
switch ($operation) {
case 'add':
$result =
add($value1, $value2);
break;
case 'subtract':
$result =
subtract($value1, $value2);
break;
case 'multiply':
$result =
multiply($value1, $value2);
break;
case 'divide':
$result =
divide($value1, $value2);
break;
default:
$result =
"Invalid operation";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple
Calculator</title>
</head>
<body>
<h1>Simple
Calculator</h1>
<form
method="post" action="">
<label
for="value1">Value 1:</label>
<input
type="number" name="value1" required><br>
<label
for="value2">Value 2:</label>
<input
type="number" name="value2" required><br>
<label
for="operation">Select Operation:</label>
<select
name="operation">
<option
value="add">Add</option>
<option
value="subtract">Subtract</option>
<option
value="multiply">Multiply</option>
<option
value="divide">Divide</option>
</select><br>
<input
type="submit" name="calculate"
value="Calculate">
</form>
<?php if
(isset($result)) : ?>
<h2>Result:
<?php echo $result; ?></h2>
<?php endif; ?>
</body>
</html>
No comments:
Post a Comment