An operator is a symbol that tells the compiler to execute specific arithmetic or logical operations. It usually forms an expression together with the operands. we often see that it participates in mathematical or logical operations. PHP also contains many operators. This article details the important comparison operators.
Comparison operator type
As their names imply, two values can be compared. Comparison operators include the following:
1) $ a> $ B greater than: if $ a is strictly greater than $ B, TRUE is returned.
2) $ a <$ B is less than: if $ a is strictly less than $ B, TRUE is returned.
3) $ a >=$ B greater than or equal to: if $ a is greater than or equal to $ B, TRUE is returned.
4) $ a <= $ B is less than or equal to: if $ a is less than or equal to $ B, TRUE is returned.
5) $ a <> $ B is not equal to: if $ a is not equal to $ B, TRUE is returned.
6) $! = $ B is not equal to: if $ a is not equal to $ B, TRUE is returned (same as above)
7) $ a ==$ B equals: if $ a equals $ B, TRUE is returned.
8) $ a ===$ B all equals: if $ a is equal to $ B and their types are the same, TRUE is returned.
9) $! = $ B not all equal to: if $ a is not equal to $ B, or they are of different types, TRUE is returned.
Specifically, we need to distinguish between "equal" and "all equal". $ a = $ B only compares the values of the two variables, the sum is equal to comparing the values of the expressions on both sides of the operator with the data type at the same time. only the values on both sides are equal, and the calculation result is "true ". For example, $ a = 2; var_dump ($! = 2); the return value of this expression is "false" because 2 is equal to 2. In addition, $ a = 2; is an integer and var_dump ($! = 2); 2 is also an integer, but the operator is not equal "! = "So the result is false, because 2 is equal to 2. In this case, $ a = 2; var_dump ($! = '2'); the calculation result is "true", because 2 is not equal to '2', and '2' is followed by a string '2 ', that is, not all values are equal to not only variable values, but also the data types of variables.
Compare different types of results in PHP
If the PHP comparison operator compares an integer and a string, the string is converted to an integer and then compared. If two numeric strings are compared, they are compared as integers. In addition, this rule applies to switch statements.
For example:
Var_dump (0 = "a"); // return TRUE, "a" is converted to 0
Var_dump ("1" = "01"); // returns TRUE as an integer.
String, null, and string comparison: convert NULL to "" for comparison of numbers or strings
Comparison between bool and null: convert to bool, FALSE <TRUE
Object built-in classes can define their own comparisons. different classes cannot be compared. if the same class is used, properties are compared.
Comparison between string, resource, or number: converts string and resource to numbers and compares them by common mathematics.
Comparison between arrays: an array with fewer members is small. if the keys in Operation 1 do not exist in Operation 2, the arrays cannot be compared. you need to compare values one by one (see the following code)
Array is always larger than any other type
Object is always larger than any other type
Array comparison code:
Function standard_array_compare ($ op1, $ op2) {if (count ($ op1) <count ($ op2) {// small return-1 array with fewer members; // $ op1 <$ op2} elseif (count ($ op1)> count ($ op2) {return 1; // $ op1> $ op2} foreach ($ op1 as $ key => $ val) {if (! Array_key_exists ($ key, $ op2) {return null;} else if ($ val <$ op2 [$ key]) {return-1 ;} elseif ($ val> $ op2 [$ key]) {return 1 ;}} return 0; // $ op1 ==$ op2}
Comparison operators:
Expression (expr1 )? (Expr2): (expr3) when the expression expr1 is TRUE, the value is expr2, and when the expression expr1 is FALSE, the value is expr3.
The above is the key content of the comparison operators. after analyzing these operators and comparison rules, we will use more exercises to master and digest these contents.