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.
Result of php executing
Full code of calculate.php.php
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Результат обчислення</title>
- </head>
- <body>
- <h1>Результат обчислення</h1>
- <?php
- if (isset($_POST['num1']) && isset($_POST['operator']) && isset($_POST['num2'])) {
- $num1 = floatval($_POST['num1']);
- $operator = $_POST['operator'];
- $num2 = floatval($_POST['num2']);
- $result = '';
- switch ($operator) {
- case '+':
- $result = $num1 + $num2;
- break;
- case '-':
- $result = $num1 - $num2;
- break;
- case '*':
- $result = $num1 * $num2;
- break;
- case '/':
- if ($num2 != 0) {
- $result = $num1 / $num2;
- } else {
- $result = 'Ділення на нуль неможливе';
- }
- break;
- default:
- $result = 'Невідома операція';
- }
- echo "<p>Результат: $result</p>";
- } else {
- echo "<p>Будь ласка, заповніть всі поля форми.</p>";
- }
- ?>
- </body>
- </html>