PHP is easy to ignore and error trap number and string comparison _ PHP Tutorial

Source: Internet
Author: User
PHP is easy to ignore and error trap numbers are compared with strings. 0 returns true if it is compared with any character leading by a non-number (or character that cannot be converted to a number) (operator. the reason is that when comparing numbers with strings, first try to convert strings

0 returns true if it is compared with any character leading by a non-digit character (or character that cannot be converted to a number) (operator =.

The reason is that when a number is compared with a string, the string is first converted to a number before Comparison. a string that cannot be converted to a number is converted to 0. therefore, returns true when compared with 0.

More detailed comparison rules, multiple types of comparison rules, inPHP manual/language reference/operator/comparison operatorYou can find.

In PHP, when two numeric strings (strings containing only numbers) are compared, they are directly converted to numerical values for comparison.
Example: (note that the last two variables $ a and $ B are not equal)

The code is as follows:


// Example 1
$ A = '000000 ';
$ B = '000000 ';
If ($ a = $ B ){
Echo 'equal ';
} Else {
Echo 'notequal ';
}
?>


Running the above program finds that the result is equal (not the result we think)

Add a letter a to $ a and $ B respectively.

The code is as follows:


// Example 2
$ A = 'a0000203199106034578 ';
$ B = 'a0000203199106034579 ';
If ($ a = $ B ){
Echo 'equal ';
} Else {
Echo 'notequal ';
}
?>


The output is notEqual (correct result)

Example 1 is equal because PHP converts two numeric strings to a numeric string, and the two numbers are exactly the same.

The code is as follows:


$ A = 511203199106034578;
$ B = 511203199106034579;
Echo $ a; // output 5.1120319910603E + 17, that is, 511203199106030000
Echo $ B; // output 5.1120319910603E + 17, that is, 511203199106030000
?>


So the result we get in Example 1 is equal.

To avoid this unexpected result, use the type comparison operator ===( if $ a is equal to $ B and their types are the same)

The code is as follows:


// Example 4
$ A = '000000 ';
$ B = '000000 ';
If ($ a ===$ B ){
Echo 'equal ';
} Else {
Echo 'notequal ';
}
?>


In this way, we can get the expected notEqual.

Returns true if the http://www.bkjia.com/PHPjc/324586.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324586.htmlTechArticle0 is compared with a leading string (operator =) other than a number (or a character that cannot be converted to a number. the reason is that when the number is compared with the string, first try to convert 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.