PHP easy to ignore error trap number and string comparison _php tutorial

Source: Internet
Author: User

0 returns True if the leading string comparison (operator = = =) is associated with any non-numeric (or character that cannot be converted to a number).

The reason is that when a number is compared to a string, the first attempt is to convert the string to a number, then compare it to a string that cannot be converted to a number, and the result is 0, so that the comparison with 0 always returns TRUE.

More detailed comparison rules, multiple types of comparison rules,PHP Manual/Language Reference/Operator/comparison operatorcan be found.

In PHP, when comparing two numeric strings (strings with numbers only), they are converted directly into numerical values.
The following example: (Note $ A and $b the last of the two variables are not equal)
Copy CodeThe code is as follows:
Example 1
$a = ' 511203199106034578 ';
$b = ' 511203199106034579 ';
if ($a = = $b) {
echo ' equal ';
} else {
Echo ' notequal ';
}
?>

Run the above program and find the result is equal (not what we think)

We'll add a $ A to $b and a letter A to each one.

Copy CodeThe code is as follows:
Example 2
$a = ' a511203199106034578 ';
$b = ' a511203199106034579 ';
if ($a = = $b) {
echo ' equal ';
} else {
Echo ' notequal ';
}
?>

This time the output is notequal (the correct result)

Example 1 is equal because PHP converts two numeric strings into a digital type, and these two numbers are just equal to the following example
Copy CodeThe code is as follows:
$a = 511203199106034578;
$b = 511203199106034579;
echo $a; Output 5.1120319910603E+17 is 511203199106030000
Echo $b; Output 5.1120319910603E+17 is 511203199106030000
?>

So the result we got in Example 1 is equal.

The case for avoiding this unintended result is to use the type comparer = = = As in the following example (if $a equals $b and they are of the same type)
Copy CodeThe code is as follows:
Example 4
$a = ' 511203199106034578 ';
$b = ' 511203199106034579 ';
if ($a = = = $b) {
echo ' equal ';
} else {
Echo ' notequal ';
}
?>

So we can get the notequal we're expecting.

http://www.bkjia.com/PHPjc/324586.html www.bkjia.com true http://www.bkjia.com/PHPjc/324586.html techarticle 0 Returns True if the leading string comparison (operator = = =) is associated with any non-numeric (or character that cannot be converted to a number). The reason is that when a number is compared to a string, the first attempt is to move the string ...

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