PHP veteran new people come to see this question about this interesting

Source: Internet
Author: User
PHP veteran new people come to see it this question is interesting
$id 1 = "42022219860323211";
$id 2 = "42022219860323210";
if ($id 1== $id 2) {
echo "The two identity cards are the same";
}else{
echo "These two identity cards are not the same";



Theoretically, it will output
These two IDs are different.

But the actual output is a bit out.


I and another colleague of the Web environment are output the same two IDs
A colleague of mine is normal, and these two IDs are different.

These two variables are printed out as String

is PHP setup different?
------to solve the idea----------------------
Then use = = = OK.
------to solve the idea----------------------
Print the next Php_int_max to see if two numbers exceed the range of integers allowed in PHP

It is recommended to compare by = = =.
------to solve the idea----------------------
Seems to have found a reason.
The comparison operators in PHP are a bit weird and prone to errors, and now the comparison rules are listed:

1, when the size comparison of two characters, is compared to the ASCII size of the two characters-this article is easy to understand.

2, when the size comparison of two strings, is starting from the first character, respectively, the corresponding ASCII size, as long as from a corresponding position, where one string of the current position of the character is greater than the other string corresponding position character, that is, the direct identification of the two string size, such as ' ba ' > ' AZ '-This is what we all know.

Then ' 10 ' compared with ' a ', of course, the same, first of ' 1 ' and ' a ' ASCII code comparison, ' a ' large.

3, when a number and a string/character size comparison, the system first attempt to convert this string/character to integer/float, and then compare, such as ' 12bsd ' transformation to 12, ' A ' transformation to 0, it is important to note that the corresponding ASCII code value is not compared to the size of the number.

In fact, the same reason, ' a ' +10 result is also 10.

and easy to ignore: 0 and any string comparison that is not converted to a number (operator = = =), returns TRUE.

Finally, the following results will appear:

1 var_dump (' 1000000 ' < ' a '); Result:boolean true
2 Var_dump (' a ' <1); Result:boolean true
3 Var_dump (1< ' 1000000 '); Result:boolean true

There is a little bit different from the conventional thinking.
  • Related Article

    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.