2014-01-24 47 views
0

我犯了一个PHP的形式,并创造了这个错误处理脚本:显示文本GET

<?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>"; 
     } 
    } 
?> 

我试图与“badpass”的GET值运行。出于某种原因,它回应了'没有填充'的信息。

我正在学习这一点,我找不到解决方案。你可以快速查看,看看有什么不对?

+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>'; } 

甚至更​​好(更DRY),只需使用一个数组而不是所有'if'中的'=='GET –

+1

在您的条件下使用== –

+1

或者,您应该始终使用“===”,除非您有理由不这样做。 – ragol

回答

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”默认备用。

希望这有助于!

+0

+1是IMO唯一正确的答案。但是,请确保检查数组的键是否存在(查看我的答案以了解我的意思)。 –

+0

够好!如果数组键中不存在'$ _GET [“alert”]'会发生什么?如果可以用'array_key_exist()'检查并使用它,会更好。 –

+0

谢谢你。 Y –

0

的原因,它默认为'nofilled'消息,是因为你使用的=赋值运算符,而不是在==所有conditional statements

=assignment operator,而==是检查它的“等于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>"; 
     } 
    } 
?> 

请教关于比较操作手册:

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>"; 
      } 
     } 
+0

'$ alert == $ _GET [“alert”];'?? –

+0

好! '$ alert = $ _GET [“alert”];'。你分配了'$ alert'变量而不使用它? –

+0

别担心,它不是我的代码。 SO会在需要的地方使用它。 –

0

=是一个赋值操作符。因此$_GET["alert"]="nofilled""nofilled"指定为$_GET["alert"]。相反,您应该使用===这是一个身份比较运算符。除非您有很强的理由,否则不应使用==进行比较。 ==会自动进行类型转换,导致通常很难检测到的错误。由于转换它也稍微(但当然可以忽略)较慢。

1

这看起来更像是在使用开关的情况:因为使用了`

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>'; 
    }