php实验3.3

<?php
$sum = 0;
if (!empty($_POST)) {
$n = $_POST['num'];
for ($i = 1; $i <= $n; $i++) {
$sum = $sum + $i;
}
echo "<script>alert('1+2+..+$n=$sum')</script>";

}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>计算累加和</title>
</head>
<body>
<h3 style="color: darkblue">计算累加和</h3>
<form action="" method="post">
<div>
1+2+3+...+<input type="text" name="num"> <input type="submit" value="完成">
</div>
</form>
</body>
</html>

posted @ 2022-05-11 00:03  Lindseyyip  阅读(19)  评论(0编辑  收藏  举报