views:

59

answers:

3

Hello, this may seem like a stupid question, but it is stumping me nontheless. I'm sure that the answer is something small. I think it's just one of those situations where I have been looking at the code for too long.

I am trying to compare two PHP variables to see if they are the same. As you can see below, I am comparing $verification_answer with strrev(date("Ymd")) which is today's date, reversed. So today, $verification_answer would be 31700102. Every time I try to do the comparison, however, the if statement executes (as a non-match).

$verification_answer = strrev(date("Ymd"));

if($verification != $verification_answer){
     $failed .= "<h2>Attention:</h2><p>The verification code is incorrect. Please try again.</p>"; 
}

Can anyone see the issue? Thanks!


UPDATE: $verification is from HTML user input:

$verification = mysql_escape_string($_POST['verification']);
+3  A: 

I am comparing $verification_answer with strrev(date("Ymd"))

If that's actually what you intended to do, I think you messed up the name of the variable in the first line; it should be:

$verification = strrev(date("Ymd"));

If you accidentally overwrote the value of $verification_answer and used $verification in a comparison when it's undefined, the comparison will always be false. PHP will emit a warning, but if you have them disabled you won't see it

Michael Mrozek
Woops, that was just a typo while typing the post.
behrk2
+6  A: 

There's nothing wrong with the if statement. Display the two values and you should see some sort of difference:

var_dump($verification);
var_dump($verification_answer);

Perhaps $verification doesn't contain what you think it does, or you misspelled it earlier and assigned to a different variable, or...

John Kugelman
A: 

I must have spelled verification wrong somewhere...I copy and pasted 'verification' over each existing variable name and it fixed the problem. Thanks!

behrk2
If you turn on warnings in `php.ini` while you're developing it'll help avoid this sort of thing
Michael Mrozek