php长数字在字符串比较 [英] php long numbers in string comparison

查看:96
本文介绍了php长数字在字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速简单问题:

var_dump("555555555555555555555" == "555555555555555555553"); //bool(true)
var_dump("aaaaaaaaaaaaaaaaaaaaa" == "aaaaaaaaaaaaaaaaaaaab"); //bool(false)

为什么会发生这种情况?

Why does this happen?

我知道我可以使用

var_dump(strcmp("555555555555555555555", "555555555555555555553") == 0); //bool(false)

但是为什么第一行返回true?

But why the first row returns true?

推荐答案

这是类型强制的副作用。
有一个关于phpsadness的文章
基本上,比较中的字符串被转换为数字类型,并且由于精度损失,看起来是相等的。

It's a side effect of type-coercing. There's an article on phpsadness about it. Basically, the strings in the comparison are converted to numeric types, and due to precision loss, appear to be equal.

这篇关于php长数字在字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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