Simple PHP calculator

Sure! Here is a simple calculator implemented in PHP:

<?php

// Check if the form has been submitted
if (isset($_POST['submit'])) {
  // Get the values from the form
  $num1 = $_POST['num1'];
  $num2 = $_POST['num2'];
  $operator = $_POST['operator'];

  // Check which operator was chosen and perform the corresponding calculation
  if ($operator == 'add') {
    $result = $num1 + $num2;
  } elseif ($operator == 'subtract') {
    $result = $num1 - $num2;
  } elseif ($operator == 'multiply') {
    $result = $num1 * $num2;
  } elseif ($operator == 'divide') {
    $result = $num1 / $num2;
  }
} ?>

<form action="calculator.php" method="post">
  <input type="text" name="num1">
  <select name="operator">
    <option value="add">+</option>
    <option value="subtract">-</option>
    <option value="multiply">*</option>
    <option value="divide">/</option>
  </select>
  <input type="text" name="num2">
  <input type="submit" name="submit" value="Calculate">
</form>

<?php // If the form has been submitted, display the result
if (isset($result)) {
  echo "Result: $result";
}
?>

Watch a course Learn object oriented PHP

This code creates a simple form with two text inputs for the numbers and a dropdown menu for the operator. When the form is submitted, the PHP code retrieves the values from the form and performs the appropriate calculation. Finally, it displays the result on the page.

I hope this helps! Let me know if you have any questions.