Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
// Инициализация переменных
$num1 = $num2 = $num3 = 0;
$sum = 0;
$formSubmitted = false;
// Проверка отправки формы
if (isset($_POST['submit'])) {
$formSubmitted = true;
$num1 = isset($_POST['num1']) ? (float)$_POST['num1'] : 0;
$num2 = isset($_POST['num2']) ? (float)$_POST['num2'] : 0;
$num3 = isset($_POST['num3']) ? (float)$_POST['num3'] : 0;
$sum = $num1 + $num2 + $num3;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Калькулятор суммы</title>
</head>
<body>
<h2>Сумма трёх чисел</h2>
<form method="post" action="">
Число 1: <input type="number" step="any" name="num1" value="<?= $num1 ?>"><br><br>
Число 2: <input type="number" step="any" name="num2" value="<?= $num2 ?>"><br><br>
Число 3: <input type="number" step="any" name="num3" value="<?= $num3 ?>"><br><br>
<input type="submit" name="submit" value="Вычислить сумму">
</form>
<?php if ($formSubmitted): ?>
<h3>Результат: <?= $num1 ?> + <?= $num2 ?> + <?= $num3 ?> = <?= $sum ?></h3>
<?php endif; ?>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num1 = isset($_POST['num1']) ? (int)$_POST['num1'] : 0;
$num2 = isset($_POST['num2']) ? (int)$_POST['num2'] : 0;
$num3 = isset($_POST['num3']) ? (int)$_POST['num3'] : 0;
$sum = $num1 + $num2 + $num3;
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Форма для суммы чисел</title>
</head>
<body>
<h1>Введите три числа:</h1>
<form method="POST">
<label for="num1">Число 1:</label>
<input type="number" name="num1" id="num1" required><br><br>
<label for="num2">Число 2:</label>
<input type="number" name="num2" id="num2" required><br><br>
<label for="num3">Число 3:</label>
<input type="number" name="num3" id="num3" required><br><br>
<button type="submit">Отправить</button>
</form>
<?php
if (isset($sum)) {
echo "<h2>Сумма чисел: $sum</h2>";
}
?>
</body>
</html>