简体   繁体   English

使用==和===运算符在php中进行比较

[英]comparison in php using == and === operators

In my application, there are values generated at run time like x, 1x,2,1 etc. I need to compare these: 在我的应用程序中,有一些在运行时生成的值,例如x,1x,2、1等。我需要对它们进行比较:

as we know, 据我们所知,

        x  == 1  is false   
       '1x'== 1  is true
       '1' == 1  is true 

for my application, the first,second cases needs to be false and third needs to be true. 对于我的应用程序,第一,第二种情况必须为假,第三种情况必须为真。 if I use ===, 如果我使用===,

       x  === 1    is false   
      '1x'=== 1    is false   
      1'  === 1    is false  

So both these comparisons i can not use. 所以这两个比较我不能使用。

I think converting to string and then comparing using strcmp will be the best option I have. 我认为转换为字符串,然后使用strcmp进行比较将是我最好的选择。 Please share your thoughts. 请分享您的想法。 Any reply is appreciated. 任何答复表示赞赏。

strcmp would be suitable for this.But you should be aware of its return values. strcmp可能适用于此。但是您应该知道它的返回值。

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

or you can also use Regex 或者你也可以使用正则Regex

Use either strcmp or strcasecmp : 使用strcmpstrcasecmp

Returns < 0 if str1 is less than str2; 如果str1小于str2,则返回<0;否则,返回0。 > 0 if str1 is greater than str2, and 0 if they are equal. 如果str1大于str2,则> 0;如果相等,则> 0。 Reference 参考

Please try the following : 请尝试以下方法:

if(strlen($x) == 1) {
    if ((intval($x) === 1)) {
        echo "Equals";
    }
    else
   {
        echo "Not Equals";
   }
}
   }else
   {
       echo "Not Equals";
   }

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

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