我制作了一个PHP表单并创建了这个错误处理脚本:
<?php
if(isset($_GET["alert"])) {
$alert = $_GET["alert"];
if($_GET["alert"]="nofilled") {
echo "<div class='error'>You missed something!</div>";
}
elseif($_GET["alert"]="badpass") {
echo "<div class='error'>Your passwords don't match!</div>";
}
elseif($_GET["alert"]="badusername") {
echo "<div class='error'>Your username is too long!</div>";
}
elseif($_GET["alert"]="shortpass") {
echo "<div class='error'>Your password is too short!</div>";
}
elseif($_GET["alert"]="takenusername") {
echo "<div class='error'>That username is taken!</div>";
}
elseif($_GET["alert"]="takenemail") {
echo "<div class='error'>That email already has an account attached to it!</div>";
}
}
?>
我尝试使用GET值'badpass'来运行它。出于某种原因,它回应了'nofilled'消息。
我正在学习这个,但我找不到解决方案。你能快速看一下,看看有什么不对吗?
答案 0 :(得分:1)
您的问题发生是因为您使用的是'='而不是'=='(1是分配,2是比较)。 但是,我建议使用以下方法,因为它更有效,更清洁:
if(isset($_GET["alert"])) {
$alerts = array(
"nofilled"=>"You missed something!",
"badpass"=>"Your passwords don't match!",
"badusername"=>"Your username is too long!",
"shortpass"=>"Your password is too short!",
"takenusername"=>"That username is taken!",
"takenemail"=>"That email already has an account attached to it!"
);
echo "<div class='error'>". isset($alerts[$_GET['alert']]) ? $alerts[$_GET['alert']] : $alerts[$_GET['nofilled']]. "</div>";
}
$alerts[$_GET['alert']])
不存在的情况下添加了默认回退'nofilled'。希望这有帮助!
答案 1 :(得分:1)
这看起来更像是使用开关的情况:
<?php
if(isset($_GET["alert"])) {
echo '<div class = "error">';
switch($_GET['alert']) {
case "nofilled": echo "You missed something!"; break;
case "badpass": echo "Your passwords don't match!"; break;
...
default: echo "An undetermined error ocurred"; break;
}
echo '</div>';
}
甚至更好(更干),只需使用数组:
if (isset($_GET["alert"])) {
$Errors = array(
"nofilled" => "You missed something!",
"badpass" => "Your passwords don't match!",
...
);
echo '<div class = "error">';
if (array_key_exists($_GET['alert'], $Errors))
echo $Errors[$_GET['alert']];
else
echo "An undetermined error ocurred";
echo '</div>';
}
答案 2 :(得分:0)
默认为'nofilled'
消息的原因是因为您在所有conditional statements
==
>
=
是assignment operator
,而==
是检查&#34; 是否等于&#34; comparison operator
<强>重写:强>
<?php
if(isset($_GET["alert"])) {
$alert = $_GET["alert"];
if($_GET["alert"]=="nofilled") {
echo "<div class='error'>You missed something!</div>";
}
elseif($_GET["alert"]=="badpass") {
echo "<div class='error'>Your passwords don't match!</div>";
}
elseif($_GET["alert"]=="badusername") {
echo "<div class='error'>Your username is too long!</div>";
}
elseif($_GET["alert"]=="shortpass") {
echo "<div class='error'>Your password is too short!</div>";
}
elseif($_GET["alert"]=="takenusername") {
echo "<div class='error'>That username is taken!</div>";
}
elseif($_GET["alert"]=="takenemail") {
echo "<div class='error'>That email already has an account attached to it!</div>";
}
}
?>
参考比较运算符手册:
答案 3 :(得分:0)
=
用于分配内容
==
用于比较
在您的条件中使用==
if(isset($_GET["alert"])) {
$alert = $_GET["alert"];
if($_GET["alert"]=="nofilled") {
echo "<div class=='error'>You missed something!</div>";
}
elseif($_GET["alert"]=="badpass") {
echo "<div class=='error'>Your passwords don't match!</div>";
}
elseif($_GET["alert"]=="badusername") {
echo "<div class=='error'>Your username is too long!</div>";
}
elseif($_GET["alert"]=="shortpass") {
echo "<div class=='error'>Your password is too short!</div>";
}
elseif($_GET["alert"]=="takenusername") {
echo "<div class=='error'>That username is taken!</div>";
}
elseif($_GET["alert"]=="takenemail") {
echo "<div class=='error'>That email already has an account attached to it!</div>";
}
}
答案 4 :(得分:0)
=
是一个赋值运算符。因此,$_GET["alert"]="nofilled"
会将"nofilled"
分配给$_GET["alert"]
。相反,您应该使用===
这是一个身份比较运算符。除非有充分理由,否则不应使用==
进行比较。 ==
执行自动类型转换,导致通常很难检测到的错误。由于转换,它也略微(但当然可以忽略不计)慢。