Here is a basic example of how you can create an online calculator using PHP.
<html>
<head>
<title>Online Calculator</title>
</head>
<body>
<form method="POST">
<input type="text" name="num1" placeholder="Enter first number">
<input type="text" name="num2" placeholder="Enter second number">
<select name="operator">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<br>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
<p>The answer is:</p>
if (isset($_POST['submit'])) {
$result1 = $_POST['num1'];
$result2 = $_POST['num2'];
$operator = $_POST['operator'];
switch ($operator) {
case "None":
echo "You need to select an operator!";
break;
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
</body>
</html>
This code creates a simple form with two input fields for the numbers and a drop-down menu for the operator. When the user submits the form, the PHP code retrieves the values of the input fields and the selected operator, and performs the appropriate calculation. The result of the calculation is then displayed on the page.
- Advertisement -
Note that this is just a basic example, and there are many ways you could improve the code, such as by adding error handling or by using more advanced PHP features.