6t - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of 6t.php

  1. <?php
  2. if(isset($_POST['submit'])) {
  3.   $num1 = $_POST['num1'];
  4.   $num2 = $_POST['num2'];
  5.   $operator = $_POST['operator'];
  6.  
  7.   switch($operator) {
  8.     case "add":
  9.       $result = $num1 + $num2;
  10.       break;
  11.     case "subtract":
  12.       $result = $num1 - $num2;
  13.       break;
  14.     case "multiply":
  15.       $result = $num1 * $num2;
  16.       break;
  17.     case "divide":
  18.       if($num2 == 0) {
  19.         $result = "Error: Cannot divide by zero";
  20.       } else {
  21.         $result = $num1 / $num2;
  22.       }
  23.       break;
  24.     default:
  25.       $result = "Error: Invalid operator";
  26.   }
  27. }
  28. ?>
  29.  
  30. <!DOCTYPE html>
  31. <html>
  32. <head>
  33.   <title>Calculator</title>
  34. </head>
  35. <body>
  36.   <form method="post" action="">
  37.     <input type="number" name="num1" required>
  38.     <select name="operator" required>
  39.       <option value="add">+</option>
  40.       <option value="subtract">-</option>
  41.       <option value="multiply">*</option>
  42.       <option value="divide">/</option>
  43.     </select>
  44.     <input type="number" name="num2" required>
  45.     <input type="submit" name="submit" value="Calculate">
  46.   </form>
  47.   <?php if(isset($result)) echo "Result: " . $result; ?>
  48. </body>
  49. </html>
File Description
  • 6t
  • PHP Code
  • 21 May-2023
  • 1.09 Kb
You can Share it: