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

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

问题描述

我很难解决我的问题.我以前注意到了这一点,但直到今天才开始注意. 我试图编写自己的整数字符串检查.我知道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输出为true,因为我期望通过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.

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

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.

推荐答案

这不是bug,而是功能.任何字符串都可以转换为整数,但是如果字符串不是以整数值开头,则强制类型转换将返回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天全站免登陆