类型转换和与松散运算符“=="的比较 [英] Type casting and Comparison with Loose Operator "=="

查看:36
本文介绍了类型转换和与松散运算符“=="的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题让我非常困惑.我之前注意到了这一点,但直到今天才注意到这一点.我试图编写自己的整数字符串检查.我知道 is_numeric() 但这还不够,因为它将 float 视为数字,而不仅仅是 integersis_int() 不适用于字符串数字.

I have a problem baffling me terribly. I noticed this before but didn't give it any heed until today. I was trying to write my own check for integer strings. I know of is_numeric() but it does not suffice since it counts float as numeric not only integers and is_int() which does not work on string numbers.

我做过类似的事情


$var1 = 'string';
$var2 = '123'; 

var_dump( (int)$var1 == $var1);// boolean true 
var_dump((int)$var2 == $var2);// boolean true

 var_dump((int)$var1);//int 0
 var_dump($var1);//string 'string' (length=6)

正如预期的那样,第二个 var dump 输出为真,因为我希望通过 php 的松散比较,字符串和整数版本是相等的.

As expected the second var dump outputs true since I expect with php's loose comparison that the string and integer versions be equal.

但是对于第一个,我不明白为什么会这样.我试过强制转换为 bool 并且它仍然给我相同的结果.

However with the first, I don't get why this is so. I have tried casting to bool and it still gives me the same result.

我尝试将强制转换变量分配给新的变量并比较两者,结果仍然相同

I have tried assigning the cast var to a new variablr and comparing the two, still the same result

这是我做错了还是 php 错误?

Is this something I am doing wrong or it is a php bug?

***注意我不是在这里比较类型.我实际上试图利用 int 0 不等于 string 'string' 的事实.

***Note I am not comparing types here. I'm actually trying to take advantage of the fact that int 0 is not equal to string 'string'.

我以不同的方式编写了我的整数检查,所以我真的不需要替代方案.

I wrote my integer check differently so I don't really need alternatives for that.

***编辑我做了一些额外的检查,结果证明 0 == 'string' 也是正确的.这怎么可能?

***Edit I did some extra checking and it turns out that 0 == 'string' is true as well. How is that possible?

***编辑 2问题下面有多个正确答案.感谢所有回答的人.

***Edit 2 There are multiple correct answers below to the question. Thanks to everyone who answered.

推荐答案

这不是错误,而是功能.任何字符串都可以转换为整数,但 如果字符串不是以整数值开头,则转换将返回 0.此外,当比较整数和字符串时,将字符串转换为整数,然后根据两个整数进行检查.由于该规则,几乎任何随机字符串都等于"为零.(要绕过这种行为,您应该使用 strcmp,因为它通过将传递给字符串的任何内容进行转换来执行显式字符串比较.)

It's not a bug, it's a feature. Any string can be casted to an integer, but the cast will return 0 if the string doesn't start with an integer value. Also, when comparing an integer and a string, the string is casted to an integer and then the check is done against the two integers. Because of that rule, about just any random string is "equal" to zero. (To bypass this behavior, you should use strcmp, as it performs an explicit string comparison by casting anything passed to a string.)

为了确保我正在处理一个整数,我会先使用 is_numeric,然后将字符串转换为 int,并验证字符串化的 int 对应于输入值.

To make sure I'm dealing with an integer, I would use is_numeric first, then convert the string to an int, and verify that the stringified int corresponds to the input value.

if (is_numeric($value) && strcmp((int)$value, $value) == 0)
{
    // $value is an integer value represented as a string
}

这篇关于类型转换和与松散运算符“=="的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆