比较两个 PHP 变量时遇到问题
这似乎是一个愚蠢的问题,但它仍然困扰着我。我确信答案是一些小事。我认为这只是我长时间查看代码的情况之一。
我正在尝试比较两个 PHP 变量,看看它们是否相同。正如您在下面看到的,我将 $verification_answer
与 strrev(date("Ymd"))
进行比较,后者是今天的日期,颠倒过来。所以今天,$verification_answer
将为 31700102。但是,每次我尝试进行比较时,都会执行 if 语句(作为不匹配)。
$verification_answer = strrev(date("Ymd"));
if($verification != $verification_answer){
$failed .= "<h2>Attention:</h2><p>The verification code is incorrect. Please try again.</p>";
}
有人能看到这个问题吗?谢谢!
更新:$verification
来自 HTML 用户输入:
$verification = mysql_escape_string($_POST['verification']);
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']);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这实际上是您想要做的,我认为您弄乱了第一行中的变量名称;应该是:
如果您不小心覆盖了
$verification_answer
的值,并在未定义的情况下在比较中使用了$verification
,则比较将始终为 false。 PHP 会发出警告,但如果禁用它们,您将看不到它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:
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我一定在某个地方拼写了验证错误...我将“验证”复制并粘贴到每个现有变量名称上,它解决了问题。谢谢!
I must have spelled verification wrong somewhere...I copy and pasted 'verification' over each existing variable name and it fixed the problem. Thanks!
if
语句没有任何问题。显示这两个值,您应该会看到某种差异:也许
$verification
不包含您认为它包含的内容,或者您之前拼写错误并分配给不同的变量,或者...There's nothing wrong with the
if
statement. Display the two values and you should see some sort of difference:Perhaps
$verification
doesn't contain what you think it does, or you misspelled it earlier and assigned to a different variable, or...