Comparison of longer numeric strings

Source: Internet
Author: User
A comparison of longer numeric strings
This post was last edited by Anyilaoliu on 2013-12-13 15:04:43

Title
Encounter such a problem


$a = "222111565652521142";
$b = "222111565652521139";
$c = 222111565652521142;
$d = 222111565652521139;

Var_dump ($a);
echo "
";
Var_dump ($b);
echo "
";
Var_dump ($c);
echo "
";
Var_dump ($d);
echo "
";

if ($a = = $b) {
Echo 3;
}else{
Echo 31;
}
echo "
";

if ($c = = $d) {
Echo 4;
}else{
Echo 41;
}
echo "
";


The result is





String ' 222111565652521142 ' (length=18)


String ' 222111565652521139 ' (length=18)


Float 2.2211156565252E+17


Float 2.2211156565252E+17



31
4


The question is, how does the second case compare the two?

Two sets of numbers read from the database, the Var_dump is a string (18) But the comparison is the opposite of the example above, and the comparison of two strings is also passed.

Solving

Share to: more


------Solution--------------------
Most of the PHP environments we use are 32-bit, whereas a long integer in a 32-bit environment is represented by 4 bytes.
i.e.-pow (2,31) to POW (2,31)
Pow (2,31) = 2147483648
Pow (2,32) = 4294967296
This obviously does not satisfy the general need, so PHP has done some expansion on the basis of the Logn type. 1 integers stored in 6 bytes
That is, a positive number between 99999999999999 and 99999999999999, or a 14-bit integer.

And in 64-bit PHP can easily break this limit, because 64-bit environment, a LOGN type itself is 8 bytes



------Solution--------------------
with = = = does not convert
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.