dongshanni1611 2010-08-18 16:49
浏览 93

变量在IF语句中丢失范围/值?

I have the weirdest problem I cannot figure out, see the following code:

 $frmUsername = $_POST['frmUsername'];
 $frmPassword = $_POST['frmPassword'];

 if($frmUsername == "" || $frmPassword == "") {
  print "frmUsername: " . $frmUsername;
  print "frmPassword: " . $frmPassword;

 } exit();

The result will be:

frmUsername: frmPassword:

But if I do the same thing and move the print statements outside of the IF:

 $frmUsername = $_POST['frmUsername'];
 $frmPassword = $_POST['frmPassword'];

 print "frmUsername: " . $frmUsername;
 print "frmPassword: " . $frmPassword;

 if($frmUsername == "" || $frmPassword == "") {

 } exit();

The result will be:

frmUsername: MYUSERNAMEfrmPassword: MYPASSWORD

So, why is the IF statement thinking frmUsername and frmPassword are blank, event when they're not, example:

 $frmUsername = $_POST['frmUsername'];
 $frmPassword = $_POST['frmPassword'];

 if($frmUsername == "" || $frmPassword == "") {
      print "I think the strings are empty, even when they're not";
 } exit();

The result will be:

I think the strings are empty, even when they're not

Second example:

$frmUsername = $_POST['frmUsername'];
$frmPassword = $_POST['frmPassword'];

if($frmUsername == "" || $frmPassword == "") {
    print "I think the strings are empty, even when they're not: '$frmUsername' '$frmPassword'";
    exit();
}

The result will be:

I think the strings are empty, even when they're not: '' ''

  • 写回答

4条回答 默认 最新

  • dougan6982 2010-08-18 16:51
    关注

    So, why is the IF statement thinking frmUsername and frmPassword are blank?

    Works as designed.

    You are saying:

    if ($frmUsername is empty) OR ($frmPassword is empty), do the following: .....

    if both strings are not empty, the condition won't match.

    What you probably want is

    if($frmUsername != "" && $frmPassword != "")
    

    which will match only when both strings contain a value.

    评论

报告相同问题?