我使用此代码检查数字是偶数还是奇数。我正在学习PHP,当我运行这段代码时,它会产生一个不寻常的错误。
<html>
<body>
<head>
<title>Judging even and odd numbers</title>
</head>
<form method = "post" action="EAO.php" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" />
</form>
<?PHP
if (isset($_POST['checker'])) {
$number = $_POST['number'];
if ($number % 2 == 0 ) {
echo "The number is Even";
}
if($number % 2 == 1 ) {
echo "The number is odd";
}
if ($number == "") {
echo "Please enter a number";
}
}
?>
</body>
</html>
输入数字时效果很好但是当您将表单提交为空时,它表示&#34;请输入一个数字。这个数字甚至是#34;如果我做错了,请帮我纠正。如果我的代码不是判断数字的标准,请编写更好的代码。谢谢。
答案 0 :(得分:3)
只需颠倒你的逻辑就能为你做出神奇的事。您必须始终使用if () {....} elseif () {....}
,以便在给定时间只运行一个逻辑。
<html>
<head>
<title>Judging even and odd numbers</title>
</head>
<body>
<form method = "post" action="EAO.php" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" />
</form>
<?PHP
if (isset($_POST['checker'])) {
$number = $_POST['number'];
if ($number == "") {
echo "Please enter a number";
}
else if ($number % 2 == 0 ) {
echo "The number is Even";
}
else if($number % 2 == 1 ) {
echo "The number is odd";
}
}
?>
</body>
</html>
答案 1 :(得分:1)
试试这个:
<html>
<head>
<title>Judging even and odd numbers</title>
</head>
<body>
<form method = "post" action="" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" />
</form>
<?PHP
if (isset($_POST['checker'])) {
$number = $_POST['number'];
if(empty($number))
{
echo "Please enter a number";
}
elseif ($number % 2 == 0 ) {
echo "The number is Even";
}
else {
echo "The number is odd";
}
}
?>
</body>
</html>
在head标签后放置body标签。如果要将表单发布到同一页面,则action参数是可选的。 使用if ... elseif ... else condition http://www.w3schools.com/php/php_if_else.asp
答案 2 :(得分:1)
另一个工作版本是(改进的安全性):
<?php
if (isset($_POST['checker']) && $_POST['checker'] == 'true') {
if (empty($_POST['number'])) { // 0 or empty values will be blocked
echo "Please enter a number";
} else {
if (ctype_digit($_POST['number'])) { // only number will be accepted
$number = $_POST['number'];
if ($number % 2 == 0 ) {
echo "The number is Even";
}
if($number % 2 == 1 ) {
echo "The number is odd";
}
else {
echo 'Numbers only please!';
}
}
}
?>
答案 3 :(得分:0)
在这里,我们和你一起去代码
<html>
<body>
<head><title>Judging even and odd numbers</title></head>
<form method = "post" action="" >
<font size = "20">Please enter a number to check if it is even or odd:</font>
<input type = "text" name = "number" />
<input type = "hidden" name="checker" value="true" />
<input type = "submit" value = "submit" name="submit" />
</form>
<?PHP
if (isset($_POST['submit'])) {
$number = $_POST['number'];
if ($number % 2 == 0 ) {
echo "The number is Even";
}
if($number % 2 == 1 ) {
echo "The number is odd";
}
if ($number == "") {
echo "Please enter a number";
}
}
?>
</body>
</html>