Comparison of long numeric strings

Source: Internet
Author: User
Comparison of long numeric strings This post was last edited by anyilaoliu from 2013-12-1315: 04: 43 such as Question & nbsp; encountered such a problem $ a & nbsp; 222111565652521142; $ B & nbsp; 222111565652521139; $ c & nbsp; & nbs long numeric string comparison problem

This post was last edited by anyilaoliu at 15:04:43, January 13 ,.

Example
This problem occurs.


$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 can we compare the two in the second case?

Problem 2: The two groups of numbers read from the database are displayed as string (18) after var_dump, but the comparison result is the opposite of the above example, the comparison between the two strings is also passed.

Solution

Share: More


------ Solution --------------------
Most of the php environments we use are 32-bit, while in a 32-bit environment, a long integer is represented in 4 bytes.
That is, between-pow () and pow ()
Pow (2147483648) =
Pow (4294967296) =
This obviously cannot meet general requirements, so php has made some extensions based on the logn type. 6 bytes are used to store one integer.
That is, a positive number between-99999999999999 and 99999999999999, that is, a 14-digit integer.

In 64-bit php, this limit can be easily broken through, because in a 64-bit environment, a logn type itself is 8 bytes.



------ Solution --------------------
No conversion with ===

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.