<?php
// calculator.php
$result = '';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $num1 = $_POST['num1'] ?? 0;
    $num2 = $_POST['num2'] ?? 0;
    $operator = $_POST['operator'] ?? '+';

    if (is_numeric($num1) && is_numeric($num2)) {
        switch ($operator) {
            case '+':
                $result = $num1 + $num2;
                break;
            case '-':
                $result = $num1 - $num2;
                break;
            case '*':
                $result = $num1 * $num2;
                break;
            case '/':
                $result = ($num2 != 0) ? $num1 / $num2 : 'Division by zero!';
                break;
            default:
                $result = 'Invalid operator';
        }
    } else {
        $result = 'Please enter valid numbers.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 50px;
            background: #f4f4f4;
        }
        .calc {
            background: white;
            padding: 20px;
            border-radius: 10px;
            width: 300px;
            margin: auto;
            box-shadow: 0 2px 6px rgba(0,0,0,0.2);
        }
        input, select, button {
            width: 100%;
            margin: 8px 0;
            padding: 10px;
            font-size: 16px;
        }
        button {
            background: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
        }
        .result {
            margin-top: 15px;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="calc">
    <h2>Basic Calculator</h2>
    <form method="post">
        <input type="number" step="any" name="num1" placeholder="First number" required>
        <select name="operator">
            <option value="+">+</option>
            <option value="-">−</option>
            <option value="*">×</option>
            <option value="/">÷</option>
        </select>
        <input type="number" step="any" name="num2" placeholder="Second number" required>
        <button type="submit">Calculate</button>
    </form>

    <?php if ($result !== ''): ?>
        <div class="result">Result: <?= htmlspecialchars($result) ?></div>
    <?php endif; ?>
</div>
</body>
</html>
