php-weak types of small traps

Source: Internet
Author: User
Tags php tutorial
A question to be discussed in the group today, record it.

The origin of the problem is
Var_dump (MD5 (' 240610708 ') = = MD5 (' Qnkcdzo ')); True
A string of two md5, compared with the comparison operator (= =), is actually equal.
Output a value of two values, respectively:
MD5 (' 240610708 ')//0e462097431906509019562988736854
MD5 (' Qnkcdzo ')//0e830400451993494058024219903391
Seeing the result, the first feeling is intval, so 0 = = 0 Returns True,
Later, I was reminded that this was not the case.
PHP comparison operators:
http://php.net/manual/zh/language.operators.comparison.php
Example:

If you compare a number and a string or compare strings that involve numeric content, the strings are converted to numeric values and compared to numeric values. This rule also applies to switch statements. The type conversion is not performed when compared with = = = or!==, because the type and the value are compared at this time.
 
  Truevar_dump ("1" = = "01"); 1 = = 1-truevar_dump ("ten" = = "1e1"); Ten = = Truevar_dump (+ = "1e2"); + = Trueswitch ("a") {case 0:    echo "0";    Break;case "a"://Never reached because "a" is already matched with 0    echo "a";    break;}? >

In other words, this equality is not because two strings are intval, and become 0=0
But is numerically 0e4xxx = = 0e8xxx, that is, scientific notation, 0*10 of the N-square, that is, 0.0000 and 0.00000000
So there will be an equal result.


The above describes the php-weak type of small traps, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.