开发者

Variables losing scope/value within IF statement?

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开发者_如何学Go'];

 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: '' ''


The if-conditional in your first snippet will print only if either of the fields are blank. Also, check that you're using logical equality ('==') and not assignment ('=') in your conditional.


The first two blocks of code as written certainly do make your head spin...

Effectively the second example is proving that the POST data is in fact coming through, and that the variables are being assigned correctly (he's printing the values and they come up correctly).

But then in the first example he wants to print certain things if EITHER of them are blank. PHP seems to be printing them even though NEITHER of them are blank.

Very strange indeed.

What happens if instead of using POST data (just as a test...) you just add stuff staticly?

e.g.

$frmUsername = "user";
$frmPassword = "pass";

Does the if statement still think they are both blank?


This probably isn't the answer but it is better to type it all here rather than in a comment like I started with.

In your problem if, put an else on the end just so you can be certain when it passes and when the test fails:

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

if($frmUsername == "" || $frmPassword == "") {
    echo 'strings are empty?';    
}
else
{
    echo 'strings are not empty?';
}

Also you could print out everything in $_POST just to be certain there isn't an issue there:

echo '<pre>'.print_r($_POST).'</pre>';


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜