calculate.php - 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 calculate.php.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Результат обчислення</title>
  7. </head>
  8. <body>
  9.     <h1>Результат обчислення</h1>
  10.     <?php
  11.     if (isset($_POST['num1']) && isset($_POST['operator']) && isset($_POST['num2'])) {
  12.         $num1 = floatval($_POST['num1']);
  13.         $operator = $_POST['operator'];
  14.         $num2 = floatval($_POST['num2']);
  15.         $result = '';
  16.  
  17.         switch ($operator) {
  18.             case '+':
  19.                 $result = $num1 + $num2;
  20.                 break;
  21.             case '-':
  22.                 $result = $num1 - $num2;
  23.                 break;
  24.             case '*':
  25.                 $result = $num1 * $num2;
  26.                 break;
  27.             case '/':
  28.                 if ($num2 != 0) {
  29.                     $result = $num1 / $num2;
  30.                 } else {
  31.                     $result = 'Ділення на нуль неможливе';
  32.                 }
  33.                 break;
  34.             default:
  35.                 $result = 'Невідома операція';
  36.         }
  37.  
  38.         echo "<p>Результат: $result</p>";
  39.     } else {
  40.         echo "<p>Будь ласка, заповніть всі поля форми.</p>";
  41.     }
  42.     ?>
  43. </body>
  44. </html>
  45.  
File Description
  • calculate.php
  • PHP Code
  • 02 Oct-2023
  • 1.34 Kb
You can Share it: