tags:

views:

43

answers:

3

Hi,

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

A: 

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.

Pekka
Yes, but neither of them were empty, so it shouldn't even go into the if statement, but it will every time.
RewbieNewbie
@Rewbie but what you are saying above is that it *won't* go into the block. Aren't you?
Pekka
See the above edit I gave you as an example.
RewbieNewbie
@Rewbie strange. Can you change the 2nd example to ` print "I think the strings are empty, even when they're not: '$frmUsername' '$frmPassword'";` just to make 100% sure?
Pekka
Edited above. I think this is an issue with what's reported on screen, and what is being reloaded in Chrome when viewing source.
RewbieNewbie
Have you tried it in Firefox?
kevinmajor1
+1  A: 

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.

kevinmajor1
voteup for pointing a possible logical issue with = being used instead of ==
ovais.tariq
A: 

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?

Rafael