简体   繁体   English

相等的字符串在php上不相等

[英]Equal strings are not equal on php

Hi guys hope you can help me, I'm trying to compare two strings but I get the same result always "diferent" even when the string are equal. 大家好,希望您能为我提供帮助,我正在尝试比较两个字符串,但是即使字符串相等,我总是得到相同的结果。

here is the code: 这是代码:

$date = $row["day"];

var_dump($date);
var_dump($today);

if ($date != $today) {
    echo "<br>diferent";
} else {
    echo "<br>equals";
}

the var_dump for the two strings shows this: 两个字符串的var_dump显示如下:

string(10) "03/30/2019" this corresponds to $date string(10) "03/30/2019"对应于$date

string(309) "03/30/2019" this corresponds to $today string(309) "03/30/2019"对应于$today

As you can see both strings contain the same info but one has 10 chars and the other one 309 why is this happening? 如您所见,两个字符串都包含相同的信息,但是一个字符串包含10个字符,另一个字符串309为什么会发生这种情况? is there a way to fix it and prove they are equal? 有没有办法解决它并证明它们是平等的?

I use JavaScript to set $today 's value so this might be the issue? 我使用JavaScript设置$today的值,所以这可能是问题吗?

You can try to trim the two strings, that way any extra characters should be removed. 您可以尝试修整两个字符串,这样就应该删除所有多余的字符。

if (trim($date) != trim($today)) {

Or you can use strpos to see if $date is in $today. 或者,您可以使用strpos查看$ date是否在$ today中。

if(strpos($today, $date) !== false){
    echo "<br>equals";
}else{
    echo "<br>diferent";
}

Dear Please see below code, need to convert both format code please follow below code 亲爱的,请参见下面的代码,需要同时转换两种格式的代码,请遵循以下代码

$date = $row["day"];
$date=date('m/d/Y',strtotime($date));
$today=date('m/d/Y',strtotime($today));
var_dump($date);
var_dump($today);
if ($date != $today) {
echo "<br>diferent";
} else {
echo "<br>equals";
}

This will work properly. 这将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM