Home  >  Article  >  Backend Development  >  PHP comparison operator bug_PHP tutorial

PHP comparison operator bug_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:09:38674browse

Questions
First, give a PHP sample code. If you fully understand why the following results occur, there is no need to waste time on this blog. Here is a list of mistakes that PHP beginners may make


[php]

var_dump("abcdefg" == "0"); // bool(false)
var_dump("abdsafd" == 0); // bool(true)

var_dump("abcdefg" == "0"); // bool(false)
var_dump("abdsafd" == 0); // bool(true)
If you are not too clear about the above results, then just follow the blog and continue learning!


Comparison operator
Comparison operators, as their name implies, allow two values ​​to be compared.


Comparison Operator Example Name Result
$a == $b equals TRUE if $a equals $b
$a === $b congruent TRUE if $a is equal to $b and their types are also the same
$a != $b is not equal to TRUE if $a is not equal to $b
$a <> $b is not equal to TRUE if $a is not equal to $b
$a !== $b non-congruent TRUE if $a is not equal to $b, or their types are different
$a < $b is less than TRUE if $a is strictly less than $b
$a > $b is greater than TRUE if $a is strictly greater than $b
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b
$a >= $b is greater than or equal to TRUE if $a is greater than or equal to $b

Note:
If you compare an integer and a string, the string is converted to an integer
If comparing two numeric strings, compare as integers


At this point, we can talk about the PHP code above. var_dump("abcdefg" == "0") is false because it is a comparison of two strings, corresponding to the strcmp function of c, so it should be false. However, if var_dump("abdsafd" == 0) is true, you need to learn the rules for converting strings to integers


Convert string to integer
When a string is used in a numeric context, the result and type are as follows:


If the string does not contain '.', 'e', ​​or 'E', and the numeric value conforms to the integer type restrictions (defined by PHP_INT_MAX), the string is considered an integer, and is considered a float in other cases.


The starting part of the string gives its value. If the string starts with a legal number, this number can be used directly. Otherwise, the value is 0. Legal values ​​consist of a symbol, followed by one or more digits (possibly with a decimal point), followed by an optional exponent symbol such as 'e' or 'E', followed by one or more digits.


Sample code:


[php]

$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>

$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;              // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
This should be able to completely explain that var_dump("abcdefg" == 0) is true, because the comparison operator first forces "abcdefg" to be an integer 0, because 0==0 is true

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477619.htmlTechArticleThe first choice for questions is to give a PHP sample code. If you fully understand why the following results occur, no need No more wasting time on this blog, here is something for PHP beginners to pay attention to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn