为什么 !isset 似乎不起作用?
我是 PHP 世界的新手,并且已经制作了一个将输入值相乘的表单。但是,当我尝试验证某人是否未输入任何值来返回错误消息时,它确实会显示该消息。我的代码如下。如果您也能提出改进建议,我们将不胜感激。
<?php
$counter = 0;
if(isset($_POST["submit"])) {
$start = $_POST["start"];
$end = $_POST["end"];
$multiply = $_POST["multiplication"];
// if($_POST["start"] == "" && $_POST["end"] == "" && $_POST["multiplication"] == "") {
// print "Please enter some values";
// }
if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
print "Please enter some values";
}
// for($start;$start<$end;$start++) {
// $counter = $counter +1;
// $multiplication = $counter * $multiply;
// print "$counter <br />";
// print "$counter multiplied by $multiply = $multiplication <br />";
// }
}
?>
<html>
<head>
<title>Sample Multiplication</title>
</head>
<body>
<form name="multiply" method="post" action="multiplication_sample.php">
<input type="text" name="start" value="<?php if(isset($_POST["start"])) { print $start; } ?>">
<input type="text" name="end" value="<?php if(isset($_POST["end"])) { print $end; } ?>">
<input type="text" name="multiplication" value="<?php if(isset($_POST["multiplication"])) { print $multiply; } ?>">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])) {
for($start;$start<$end;$start++) {
$counter = $counter + 1;
$multiplication = $counter * $multiply;
print "$counter multiplied by $multiply = $multiplication <br />";
}
}
?>
</body>
</html>
I am new to the world of PHP and have put together a form that multiplies an entered value. However when I attempt to validate if a person has not entered any values to return an error message, it does display the message. My code below. Appreciate if you could also suggest improvements.
<?php
$counter = 0;
if(isset($_POST["submit"])) {
$start = $_POST["start"];
$end = $_POST["end"];
$multiply = $_POST["multiplication"];
// if($_POST["start"] == "" && $_POST["end"] == "" && $_POST["multiplication"] == "") {
// print "Please enter some values";
// }
if(!isset($_POST["start"], $_POST["end"], $_POST["multiplication"])) {
print "Please enter some values";
}
// for($start;$start<$end;$start++) {
// $counter = $counter +1;
// $multiplication = $counter * $multiply;
// print "$counter <br />";
// print "$counter multiplied by $multiply = $multiplication <br />";
// }
}
?>
<html>
<head>
<title>Sample Multiplication</title>
</head>
<body>
<form name="multiply" method="post" action="multiplication_sample.php">
<input type="text" name="start" value="<?php if(isset($_POST["start"])) { print $start; } ?>">
<input type="text" name="end" value="<?php if(isset($_POST["end"])) { print $end; } ?>">
<input type="text" name="multiplication" value="<?php if(isset($_POST["multiplication"])) { print $multiply; } ?>">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])) {
for($start;$start<$end;$start++) {
$counter = $counter + 1;
$multiplication = $counter * $multiply;
print "$counter multiplied by $multiply = $multiplication <br />";
}
}
?>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为 isset 将确保变量不是 NULL,但是“blank”与 null 不同。如果您提交包含空白值的表单,则该变量仍在设置中,它只是空的。
I think that
isset
will make sure a variable is notNULL
, however, "blank" is not the same as null. If you submit a form with blank values, the variable is still being set, it is just empty.提交表单后,输入字段的内容将发送到服务器。
如果这些输入字段为空,服务器会为每个输入获取一个空字符串——但它会得到一些东西;因此,
$_POST["start"]、$_POST["end"]、$_POST["multiplication"]
项已设置 - 即使它们仅包含空字符串。您可以检查:
if ($_POST["start"] === '')
if (trim($ _POST["start"]) === '')
空
:if (empty($_POST["start"]))
When the form is submitted, the content of the input fields is sent to the server.
If those input fields are empty, the server gets an empty string for each input -- but it gets something ; so, the
$_POST["start"], $_POST["end"], $_POST["multiplication"]
items are set -- even if they only contain empty strings.You could check :
if ($_POST["start"] === '')
if (trim($_POST["start"]) === '')
empty
:if (empty($_POST["start"]))
如果未定义字段,您的代码将在
标记出现之前在 html 中打印您的消息。大多数浏览器不会显示它或将其显示在意外的位置。
您应该将消息显示移动到 html 中用户可以看到的某个位置。
正如其他人指出的那样,除了第一次调用页面外,这些字段将具有空值,但仍然存在(因此
isset
将返回TRUE
)If the fields aren't defined your code will print your message in the html before the
<html>
tag appears. Most browsers won't display it or display it in an unexpected place.You should move the message display somewhere in the html where the user could see it.
And as other pointed out, except on the first call of the page the fields will have an empty value but still exists (and so
isset
will returnTRUE
)我希望,我理解你的意思是对的。是不是
效果不如预期?看来,您假设一个空字符串意味着什么都没有设置,什么是不正确的。
使用
empty()
或仅使用$_POST['start'] == ''
代替。I hope, I understand you right. It is
that works not as expected? It seems, that you assume an empty string means, that nothing is set, what is not true.
Use
empty()
or just$_POST['start'] == ''
instead.