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.
Result of php executing
Full code of 6t.php
- <?php
- if(isset($_POST['submit'])) {
- $num1 = $_POST['num1'];
- $num2 = $_POST['num2'];
- $operator = $_POST['operator'];
- switch($operator) {
- case "add":
- $result = $num1 + $num2;
- break;
- case "subtract":
- $result = $num1 - $num2;
- break;
- case "multiply":
- $result = $num1 * $num2;
- break;
- case "divide":
- if($num2 == 0) {
- $result = "Error: Cannot divide by zero";
- } else {
- $result = $num1 / $num2;
- }
- break;
- default:
- $result = "Error: Invalid operator";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Calculator</title>
- </head>
- <body>
- <form method="post" action="">
- <input type="number" name="num1" required>
- <select name="operator" required>
- <option value="add">+</option>
- <option value="subtract">-</option>
- <option value="multiply">*</option>
- <option value="divide">/</option>
- </select>
- <input type="number" name="num2" required>
- <input type="submit" name="submit" value="Calculate">
- </form>
- <?php if(isset($result)) echo "Result: " . $result; ?>
- </body>
- </html>