比较两个 PHP 变量时遇到问题

发布于 2024-09-09 04:43:18 字数 686 浏览 1 评论 0原文

这似乎是一个愚蠢的问题,但它仍然困扰着我。我确信答案是一些小事。我认为这只是我长时间查看代码的情况之一。

我正在尝试比较两个 PHP 变量,看看它们是否相同。正如您在下面看到的,我将 $verification_answerstrrev(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-09-16 04:43:19

我正在将 $verification_answer 与 strrev(date("Ymd")) 进行比较

如果这实际上是您想要做的,我认为您弄乱了第一行中的变量名称;应该是:

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

如果您不小心覆盖了 $verification_answer 的值,并在未定义的情况下在比较中使用了 $verification,则比较将始终为 false。 PHP 会发出警告,但如果禁用它们,您将看不到它

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

眼角的笑意。 2024-09-16 04:43:19

我一定在某个地方拼写了验证错误...我将“验证”复制并粘贴到每个现有变量名称上,它解决了问题。谢谢!

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

千鲤 2024-09-16 04:43:18

if 语句没有任何问题。显示这两个值,您应该会看到某种差异:

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

也许 $verification 不包含您认为它包含的内容,或者您​​之前拼写错误并分配给不同的变量,或者...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文